iOS最基础的主题切换

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

iOS最基础的主题切换_第1张图片

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

+(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文件)。这里只进行了简单的包装,弱鸡一枚,勿喷。

你可能感兴趣的:(iOS最基础的主题切换)