• iOS开发--换肤简单实现以及工具类的抽取


    一.根据美工提供的图片,可以有两种换肤的方案.

    <1>美工提供图片的类型一:

    <2>美工提供图片的类型二:这种分了文件夹文件名都一样的情况,拖入项目后最后用真实文件夹(蓝色文件夹).因为项目中的黄色文件夹都是虚拟不存在的,同名的文件只会保留一个

    勾选第二个:

    二.工具类的抽取 -- 以第二种图片的方案为前提,抽取的工具类

    • 头文件的实现                                                                                                          
    • .m文件的实现
       1 #import "SkinTool.h"
       2 
       3 /** 当前皮肤色系 */
       4 static NSString *_currentSkin;
       5 
       6 @implementation SkinTool
       7 // 初始化工具类时,给_currentSkin初始值
       8 + (void)initialize
       9 {
      10     [super initialize];
      11     // 偏好设置中没有值,皮肤默认给一个色系
      12     _currentSkin = [[NSUserDefaults standardUserDefaults] objectForKey:@"CurrentSkin"];
      13     if (_currentSkin == nil) {
      14         _currentSkin = @"blue";
      15     }
      16 }
      17 
      18 + (void)setCurrentSkinColor:(NSString *)skin
      19 {
      20     _currentSkin = skin;
      21     // 将当前的皮肤色系存放到偏好设置中
      22     [[NSUserDefaults standardUserDefaults] setObject:skin forKey:@"CurrentSkin"];
      23 }
      24 
      25 + (UIImage *)skinToolWithImageName:(NSString *)imageName
      26 {
      27     // 拼接当前皮肤色系图片在mainBundle中的位置
      28     NSString *imagePath = [NSString stringWithFormat:@"skin/%@/%@",_currentSkin,imageName];
      29     UIImage *image = [UIImage imageNamed:imagePath];
      30     return image;
      31 }
      32 
      33 + (UIColor *)skinToolWithLabelColor
      34 {
      35     // 拼接plist文件在mainBundle中的相对位置
      36     NSString *plistPath = [NSString stringWithFormat:@"skin/%@/bgColor.plist",_currentSkin];
      37     NSString *path = [[NSBundle mainBundle] pathForResource:plistPath ofType:nil];
      38     // 加载plist,获取存放rgb的字典
      39     NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
      40     NSString *rgbString = dict[@"labelBgColor"];
      41     // 分割rgb字符串
      42     NSArray *rgbArr = [rgbString componentsSeparatedByString:@","];
      43     NSInteger red = [rgbArr[0] integerValue];
      44     NSInteger green = [rgbArr[1] integerValue];
      45     NSInteger blue = [rgbArr[2] integerValue];
      46     // 返回颜色
      47     return [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1.0];
      48 }
      49 @end
    • 注意:控制文字颜色或者label背景颜色之类,是在每个色系文件夹中定义了plist文件,文件中规定了RGB                                                               
    • 外界调用工具类的时候,容易出现的错误(项目中出现在TabBar控制器情况下) -- 原因在于TabBar控制器的View是懒加载的,如果将设置皮肤类的代码写在ViewDidLoad中,默认只会执行一次;!!!!!!解决方案:将代码写在ViewWillAppear中      

    出现错误的图解:

      

  • 相关阅读:
    烟花散尽漫说无(參考资料)
    [Leetcode]-Majority Element
    百度移动搜索測试电面
    可删除超炫&amp;多种特效的Card视图(改造自cardsui-for-android开源项目),提供DEMO下载
    关闭 You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法
    Android Snackbar使用方法及小技巧-design
    Android Intent Action 大全
    android权限大全
    Android 判断SD卡是否存在及容量查询
    Android中蓝牙的基本使用----BluetoothAdapter类简介
  • 原文地址:https://www.cnblogs.com/gchlcc/p/5587570.html
Copyright © 2020-2023  润新知