iOS最基础的主题切换

很多app中都有夜间模式或者换肤功能,一直没有尝试过,今天写了一个简单的demo。 主要是自定义控制器,封装控件,完成导航栏,TabBar 文字颜色的切换,以及自定义Label 的文字大小,及颜色的切换。

首先,创建一个主题管理的单例类

+(id)shareInstance;

//设置主题色
-(void)setThemeColor:(UIColor *)color;
//获取主题色
-(UIColor *)getThemeColor;
//设置字体
-(void)setThemeFont:(CGFloat)fontSize;
//获取字体
-(CGFloat )getThemeFont;

自定义设置主题颜色(字体大小)、获取主题颜色(字体大小)的方法。 通过NSUserDefault 将颜色 和字体存储起来、方便下次进入时获取对应的主题

-(void)setThemeColor:(UIColor *)color{
    //还得将颜色存在本地 下次进入时保持该状态
    NSString *colorStr = [self toStrByUIColor:color];
    
    [UserDefaults setObject:colorStr forKey:@"ThemeColor"];
    
}

-(UIColor *)getThemeColor{
    //UIColor不能直接存在UserDefaults中,
    if ([UserDefaults objectForKey:@"ThemeColor"]) {
        UIColor *color = [self toUIColorByStr:[UserDefaults objectForKey:@"ThemeColor"]];
        return color;
    }
    
    return RGBA(74, 125, 112, 1.0);//默认颜色
}

-(void)setThemeFont:(CGFloat)fontSize{
    
    [UserDefaults setFloat:fontSize forKey:@"ThemeFont"];
}

-(CGFloat)getThemeFont{
    if ([UserDefaults objectForKey:@"ThemeFont"]) {
        CGFloat size = [[UserDefaults objectForKey:@"ThemeFont"] floatValue];
        return size;
    }
    return 14;
}

// 颜色 字符串转16进制
-(UIColor*)toUIColorByStr:(NSString*)colorStr{
    
    NSString *cString = [[colorStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if ([cString length] != 6) return [UIColor blackColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}

// 颜色 转字符串(16进制)
-(NSString*)toStrByUIColor:(UIColor*)color{
    CGFloat r, g, b, a;
    [color getRed:&r green:&g blue:&b alpha:&a];
    int rgb = (int) (r * 255.0f)<<16 | (int) (g * 255.0f)<<8 | (int) (b * 255.0f)<<0;
    return [NSString stringWithFormat:@"%06x", rgb];
}

在导航控制器和TabBar控制器中设置颜色,注册通知接收主题发生切换时,设置相应的颜色。

//导航控制器中
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationBar.barTintColor = [[ThemeManager shareInstance] getThemeColor];
    
    [self.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(setColor)
     name:kThemeColorChangeNotification object:nil];  
}
-(void)setColor{
    self.navigationBar.barTintColor = [[ThemeManager shareInstance] getThemeColor];
}

//TabBar控制器中 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.tabBar setTintColor:[[ThemeManager shareInstance] getThemeColor]];
    
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(setColor)
     name:kThemeColorChangeNotification object:nil];
}

//接收通知
-(void)setColor{
    [self.tabBar setTintColor:[[ThemeManager shareInstance] getThemeColor]];
    
}

//不要忘记移除通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


自定义Label 在初始化的方法中添加颜色、字体改变的通知

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self != nil) {
        [self setColor];
        [self setTextFont];
        
        [[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(themeColorNotification:)
         name:kThemeColorChangeNotification object:nil];
        
        [[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(themeFontNotification:)
         name:kThemeFontChangeNotification object:nil];
        
    }
    return self;
}

当主题发生改变时,直接发出通知,

//设置颜色
-(void)setColor{
    self.textColor = [[ThemeManager shareInstance] getThemeColor];
}
//设置字体
-(void)setTextFont{
    self.font = [UIFont systemFontOfSize:[[ThemeManager shareInstance] getThemeFont]];
}
//颜色通知
-(void)themeColorNotification:(NSNotification *)not{
    [self setColor];
}
//字体通知
-(void)themeFontNotification:(NSNotification *)not{
    
    [self setTextFont];
}

主题切换功能很多都涉及到TabBarItem的图片的切换,或者导航栏等各种复杂的转换,需要使用到的素材会很多(从别人那里看到的实际项目中都是存在对应的plist文件)。这里只进行了简单的包装,弱鸡一枚,勿喷。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 16,399评论 4 61
  • 用途 Instantiates a layout XML file into its corresponding ...
    hxg_阅读 567评论 0 0
  • 午夜时分,两个宝贝已酣然入眠。 我在床上辗转反侧,难以入睡。老公出门已经一个星期。不知道这次出去有没有筹到钱。白天...
    ccc橙子阅读 167评论 0 1
  • 秋日渐凉,满街栗子飘香。秋天食栗的好处,几乎是妇孺皆知的:健脾补肾,祛凉抗寒……但千言万语,最重要还是一句话:此时...
    那一座城阅读 720评论 0 0
  • 仔仔2岁半了,肠胃不是很好,大便不规律,距离上次拉臭臭已经4天了;便便困难,怎么使劲都出不来,有时便便上会有少量的...
    金牌育儿课阅读 413评论 0 0

友情链接更多精彩内容