很多app中都有夜間模式或者換膚功能,一直沒有嘗試過,今天寫了一個簡單的demo。 主要是自定義控制器,封裝控件,完成導航欄,TabBar 文字顏色的切換,以及自定義Label 的文字大小,及顏色的切換。

首先,創(chuàng)建一個主題管理的單例類
+(id)shareInstance;
//設置主題色
-(void)setThemeColor:(UIColor *)color;
//獲取主題色
-(UIColor *)getThemeColor;
//設置字體
-(void)setThemeFont:(CGFloat)fontSize;
//獲取字體
-(CGFloat )getThemeFont;
自定義設置主題顏色(字體大小)、獲取主題顏色(字體大?。┑姆椒?。 通過NSUserDefault 將顏色 和字體存儲起來、方便下次進入時獲取對應的主題
-(void)setThemeColor:(UIColor *)color{
//還得將顏色存在本地 下次進入時保持該狀態(tài)
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;
}
// 顏色 字符串轉(zhuǎn)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];
}
// 顏色 轉(zhuǎn)字符串(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控制器中設置顏色,注冊通知接收主題發(fā)生切換時,設置相應的顏色。
//導航控制器中
- (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;
}
當主題發(fā)生改變時,直接發(fā)出通知,
//設置顏色
-(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的圖片的切換,或者導航欄等各種復雜的轉(zhuǎn)換,需要使用到的素材會很多(從別人那里看到的實際項目中都是存在對應的plist文件)。這里只進行了簡單的包裝,弱雞一枚,勿噴。