iOS最基礎的主題切換

很多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文件)。這里只進行了簡單的包裝,弱雞一枚,勿噴。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,257評論 4 61
  • 用途 Instantiates a layout XML file into its corresponding ...
    hxg_閱讀 559評論 0 0
  • 午夜時分,兩個寶貝已酣然入眠。 我在床上輾轉(zhuǎn)反側(cè),難以入睡。老公出門已經(jīng)一個星期。不知道這次出去有沒有籌到錢。白天...
    ccc橙子閱讀 165評論 0 1
  • 秋日漸涼,滿街栗子飄香。秋天食栗的好處,幾乎是婦孺皆知的:健脾補腎,祛涼抗寒……但千言萬語,最重要還是一句話:此時...
    那一座城閱讀 704評論 0 0
  • 仔仔2歲半了,腸胃不是很好,大便不規(guī)律,距離上次拉臭臭已經(jīng)4天了;便便困難,怎么使勁都出不來,有時便便上會有少量的...
    金牌育兒課閱讀 400評論 0 0

友情鏈接更多精彩內(nèi)容