iOS夜间模式,换肤,换字体样式

作者: T_aa | 来源:发表于2016-06-29 10:01 被阅读956次

    实现方式:
    使用通知,需要变肤的控件注册这个监听,收到变化的通知之后改变做出相应的处理。
    亲自试过,将近百个的视图一起监听变化,性能还ok。

    简单实用好理解

    1.定义皮肤配置的管理YHSkinConfigManager和皮肤设置的单元对象SkinItem

    YHSkinConfigManager.hSkinItem .h

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    typedef NS_ENUM(NSInteger, kSkinType)
    {
        /** 背景色*/
        kSkinType_bg = 10,
        /** 背景色*/
        kSkinType_bg_1,
        /** 分割线*/
        kSkinType_separate,
        /** 主标题*/
        kSkinType_title_main,
        /** 副标题*/
        kSkinType_title_sub,
        /** 注释字体*/
        kSkinType_title_note,
        /** 红色字体*/
        kSkinType_title_red,
        /** 首页segment上的颜色*/
        kSkinType_title_segment_home,
        /** 导航栏上文字颜色*/
        kSkinType_title_navc,
        
        /** 主页顶部菜单的Bg*/
        kSkinType_home_head_menu_bg,
        
        /** cell的背景深色*/
        kSkinType_cell_bg_dark,
        
        /** cell的背景主页*/
        kSkinType_cell_bg_home
    
    这里可以扩展定义
    };
    
    
    
    
    
    @class SkinItem;
    
    @interface YHSkinConfigManager : NSObject
    
    + (instancetype)shareManager;
    
    @property (retain, nonatomic) NSMutableArray <SkinItem *> * dataList;
    
    /**
     *  颜色分类 图片 或者 颜色
     */
    + (id)getSkinInfoWithKeyType:(kSkinType)skinKey;
    
    - (void)updateSkinConfigList;
    
    @end
    
    
    
    
    @interface SkinItem : NSObject
    
    @property (assign, nonatomic) kSkinType itemKey;
    @property (retain, nonatomic) UIColor * itemColor;
    @property (retain, nonatomic) UIColor * itemImage;
    
    @end
    

    YHSkinConfigManager.mSkinItem .m

    #import "YHSkinConfigManager.h"
    #import "UIColor+BBXApp.h"
    #import "SettingManager.h"
    
    @implementation YHSkinConfigManager
    
    +(instancetype)shareManager
    {
        static YHSkinConfigManager * manager = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            manager = [[YHSkinConfigManager alloc] init];
            manager.dataList = [[NSMutableArray alloc] init];
            
            [manager updateSkinConfigList];
        });
        
        return manager;
    }
    
    
    - (void)updateSkinConfigList
    {
        [self.dataList removeAllObjects];
        
        SkinItem * item_bg = [[SkinItem alloc] init];
        item_bg.itemKey = kSkinType_bg;
        [self.dataList addObject:item_bg];
        
        SkinItem * item_bg_1 = [[SkinItem alloc] init];
        item_bg_1.itemKey = kSkinType_bg_1;
        [self.dataList addObject:item_bg_1];
        
        SkinItem * item_separate = [[SkinItem alloc] init];
        item_separate.itemKey = kSkinType_separate;
        [self.dataList addObject:item_separate];
        
        SkinItem * item_title_main = [[SkinItem alloc] init];
        item_title_main.itemKey = kSkinType_title_main;
        [self.dataList addObject:item_title_main];
    
        SkinItem * item_title_sub = [[SkinItem alloc] init];
        item_title_sub.itemKey = kSkinType_title_sub;
        [self.dataList addObject:item_title_sub];
        
        SkinItem * item_title_note = [[SkinItem alloc] init];
        item_title_note.itemKey = kSkinType_title_note;
        [self.dataList addObject:item_title_note];
        
        SkinItem * item_title_red = [[SkinItem alloc] init];
        item_title_red.itemKey = kSkinType_title_red;
        [self.dataList addObject:item_title_red];
        
        SkinItem * item_title_segment_home = [[SkinItem alloc] init];
        item_title_segment_home.itemKey = kSkinType_title_segment_home;
        [self.dataList addObject:item_title_segment_home];
        
        SkinItem * item_title_navc = [[SkinItem alloc] init];
        item_title_navc.itemKey = kSkinType_title_navc;
        [self.dataList addObject:item_title_navc];
        
        SkinItem * item_home_head_menu_bg = [[SkinItem alloc] init];
        item_home_head_menu_bg.itemKey = kSkinType_home_head_menu_bg;
        [self.dataList addObject:item_home_head_menu_bg];
        
        SkinItem * item_cell_bg_dark = [[SkinItem alloc] init];
        item_cell_bg_dark.itemKey = kSkinType_cell_bg_dark;
        [self.dataList addObject:item_cell_bg_dark];
        
        SkinItem * item_cell_bg_home = [[SkinItem alloc] init];
        item_cell_bg_home.itemKey = kSkinType_cell_bg_home;
        [self.dataList addObject:item_cell_bg_home];
        
        
        
        
        UIColor * skinColor = [UIColor colorWithHexString:[SettingManager skinColorList][[SettingManager shareManager].appSkin]];
        
        item_title_segment_home.itemColor = [UIColor flatOrangeColor];
        item_bg_1.itemColor = [UIColor whiteColor];
        item_bg.itemColor = [UIColor flatWhiteColor];
        item_separate.itemColor = [[UIColor flatGrayColor] colorWithAlphaComponent:0.4];
        item_title_main.itemColor = [UIColor flatBlackColorDark];
        item_title_sub.itemColor = [UIColor flatGrayColorDark];
        item_title_note.itemColor = [UIColor bbxTextNoteColor];
        item_title_red.itemColor = [UIColor bbxStarColor];
        item_cell_bg_dark.itemColor = [UIColor colorWithHexString:@"fbfbfb"];
        item_title_navc.itemColor = [UIColor whiteColor];
        item_home_head_menu_bg.itemColor = [UIColor whiteColor];
        item_cell_bg_home.itemColor = [UIColor colorWithHexString:@"f9f9f9"];
        
        switch ([SettingManager shareManager].appSkin) {
            case 0:
            {
                
            }
                break;
            case 1:
            {
                item_title_segment_home.itemColor = skinColor;
            }
                break;
            case 2://夜间模式
            {
                item_title_segment_home.itemColor = [UIColor flatOrangeColorDark];
                item_bg.itemColor = [UIColor flatBlackColor];
                item_bg_1.itemColor = [UIColor flatWhiteColorDark];
                item_title_note.itemColor = [[UIColor flatGrayColorDark] colorWithAlphaComponent:0.7];
                item_title_main.itemColor = [UIColor flatWhiteColorDark];
                item_separate.itemColor = [[UIColor flatGrayColorDark] colorWithAlphaComponent:0.1];
                item_title_navc.itemColor = [UIColor flatWhiteColorDark];
                item_home_head_menu_bg.itemColor = [UIColor flatWhiteColorDark];
                item_cell_bg_dark.itemColor = [UIColor flatBlackColorDark];
                item_cell_bg_home.itemColor = [UIColor flatBlackColorDark];
            }
                break;
            case 3://
            {
                item_title_segment_home.itemColor = skinColor;
            }
                break;
            case 4:
            {
                item_title_segment_home.itemColor = [UIColor flatOrangeColor];
            }
                break;
            case 5:
            {
                item_title_segment_home.itemColor = [UIColor flatOrangeColor];
            }
                break;
            case 6:
            {
                item_title_segment_home.itemColor = skinColor;
            }
                break;
            case 7:
            {
                item_title_segment_home.itemColor = skinColor;
            }
                break;
            case 8:
            {
                item_title_segment_home.itemColor = skinColor;
            }
                break;
            case 9:
            {
                item_title_segment_home.itemColor = skinColor;
            }
                break;
            case 10:
            {
                item_title_segment_home.itemColor = skinColor;
            }
                break;
            case 11:
            {
                item_title_segment_home.itemColor = skinColor;
            }
                break;
            case 12:
            {
                item_title_segment_home.itemColor = skinColor;
            }
                break;
            default:
        
                break;
        }
    }
    
    
    /**
     *  颜色分类
     */
    + (id)getSkinInfoWithKeyType:(kSkinType)skinKey
    {
        YHSkinConfigManager * manager = [YHSkinConfigManager shareManager];
        
        for(SkinItem * item in manager.dataList)
        {
            if(item.itemKey == skinKey)
            {
                if(item.itemColor)
                {
                    return item.itemColor;
                }
                else if (item.itemImage)
                {
                    return item.itemImage;
                }
                return nil;
            }
        }
    
        return nil;
    }
    
    
    
    
    @end
    
    
    
    @implementation SkinItem
    
    
    
    @end
    

    其中UIColor+BBXApp.h是软件中自定义的颜色,大家可以随意设置
    SettingManager.h是保存自己软件当前的皮肤类型设置、字体大小设置、字体样式设置。自己去保存设置:)

    2.定义一个皮肤控制的类别NSObject+Skin、皮肤颜色控制类SkinColorStyleModule和字体大小样式控制类SkinFontStyleModule

    NSObject+Skin .h+SkinColorStyleModule .h+SkinFontStyleModule .h

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    #import "YHSkinConfigManager.h"
    
    #define SKIN_COLOR_CHANGE_NOTIFICATION      @"SKIN_COLOR_CHANGE_NOTIFICATION"
    
    #define SKIN_FONT_STYLE_NOTIFICATION         @"SKIN_FONT_STYLE_NOTIFICATION"
    
    
    @class SkinColorStyleModule;
    @class SkinFontStyleModule;
    
    @interface NSObject (Skin)
    
    @property (retain, nonatomic) SkinColorStyleModule * skinColorStyle;
    
    @property (retain, nonatomic) SkinFontStyleModule * skinFontStyle;
    
    - (void)postSkinChangeNotifition;
    
    - (void)postskinFontStyleNotifition;
    
    @end
    
    
    
    
    @interface SkinColorStyleModule : NSObject
    
    @property (weak, nonatomic) id targetObj;
    
    @property (assign, nonatomic) kSkinType skinBgKey;
    @property (assign, nonatomic) kSkinType skinTitleKey;
    @property (assign, nonatomic) kSkinType skinTitleSelKey;
    @property (assign, nonatomic) kSkinType skinBorderKey;
    @property (assign, nonatomic) kSkinType skinImageKey;
    @property (assign, nonatomic) kSkinType skinImageSelKey;
    
    -(instancetype)initWithTarget:(id)targetObj andBlock:(void(^)(SkinColorStyleModule * obj))configBlock;
    
    @end
    
    
    
    @interface SkinFontStyleModule : NSObject
    
    @property (weak, nonatomic) id targetObj;
    
    @property (assign, nonatomic) NSInteger fontNormal;
    
    - (instancetype)initWithTarget:(id)targetObj andNormalFontSize:(NSInteger)normalSize;
    
    @end
    

    NSObject+Skin .m+SkinColorStyleModule .m+SkinFontStyleModule .m

    
    #import "NSObject+Skin.h"
    #import <objc/runtime.h>
    
    #import "SettingManager.h"
    #import "UIColor+BBXApp.h"
    #import "UIImage+BBX.h"
    #import "UIFont+BBX.h"
    #import "UINavigationBar+BGColor.h"
    
    @implementation NSObject (Skin)
    
    -(SkinColorStyleModule *)skinColorStyle
    {
        return objc_getAssociatedObject(self, @selector(skinColorStyle));
    }
    
    -(void)setSkinColorStyle:(SkinColorStyleModule *)skinColorStyle
    {
        objc_setAssociatedObject(self, @selector(skinColorStyle), skinColorStyle, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    
    -(SkinFontStyleModule *)skinFontStyle
    {
        return objc_getAssociatedObject(self, @selector(skinFontStyle));
    }
    
    -(void)setSkinFontStyle:(SkinFontStyleModule *)skinFontStyle
    {
        objc_setAssociatedObject(self, @selector(skinFontStyle), skinFontStyle, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    
    - (void)postSkinChangeNotifition
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:SKIN_COLOR_CHANGE_NOTIFICATION object:nil];
    }
    
    - (void)postskinFontStyleNotifition
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:SKIN_FONT_STYLE_NOTIFICATION object:nil];
    }
    
    
    
    
    
    
    @end
    
    
    
    
    
    #pragma mark - SkinColorStyleModule -
    
    @implementation SkinColorStyleModule
    
    -(instancetype)initWithTarget:(id)targetObj andBlock:(void (^)(SkinColorStyleModule *))configBlock
    {
        self = [super init];
        if(self)
        {
            self.targetObj = targetObj;
            
            if(configBlock)
            {
                configBlock(self);
            }
            
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(skinColorChange) name:SKIN_COLOR_CHANGE_NOTIFICATION object:nil];
            
            [self skinColorChange];
        }
        
        return self;
    }
    
    
    
    - (void)skinColorChange
    {
        id skinObjBG = [YHSkinConfigManager getSkinInfoWithKeyType:self.skinBgKey];
        id skinObjImage = [YHSkinConfigManager getSkinInfoWithKeyType:self.skinImageKey];
        id skinObjImageSel = [YHSkinConfigManager getSkinInfoWithKeyType:self.skinImageSelKey];
        id skinObjTitle = [YHSkinConfigManager getSkinInfoWithKeyType:self.skinTitleKey];
        id skinObjTitleSel = [YHSkinConfigManager getSkinInfoWithKeyType:self.skinTitleSelKey];
        id skinObjBorder = [YHSkinConfigManager getSkinInfoWithKeyType:self.skinBorderKey];
        
        if([skinObjBorder isKindOfClass:[UIColor class]] && [self.targetObj isKindOfClass:[UIView class]])
        {
            UIColor * borderColor = (UIColor *)skinObjBorder;
            UIView * v = (UIView *)self.targetObj;
            v.layer.borderColor = borderColor.CGColor;
        }
        
        if([self.targetObj isKindOfClass:[UIImageView class]])
        {
            UIImageView * imageV = (UIImageView *)self.targetObj;
            
            if([skinObjBG isKindOfClass:[UIColor class]])
            {
                imageV.backgroundColor = skinObjBG;
            }
            
            if([skinObjImage isKindOfClass:[UIImage class]])
            {
                imageV.image = skinObjImage;
            }
        }
        else if ([self.targetObj isKindOfClass:[UIButton class]])
        {
            UIButton * btn = (UIButton *)self.targetObj;
    
            if([skinObjBG isKindOfClass:[UIColor class]])
            {
                btn.backgroundColor = skinObjBG;
            }
            
            if([skinObjImage isKindOfClass:[UIImage class]])
            {
                [btn setImage:skinObjImage forState:UIControlStateNormal];
            }
            
            if([skinObjImageSel isKindOfClass:[UIImage class]])
            {
                [btn setImage:skinObjImageSel forState:UIControlStateSelected];
            }
            
            if([skinObjTitle isKindOfClass:[UIColor class]])
            {
                [btn setTitleColor:skinObjTitle forState:UIControlStateNormal];
            }
            
            if([skinObjTitleSel isKindOfClass:[UIColor class]])
            {
                [btn setTitleColor:skinObjTitleSel forState:UIControlStateSelected];
            }
        }
        else if ([self.targetObj isKindOfClass:[UILabel class]])
        {
            UILabel * lab = (UILabel *)self.targetObj;
            
            if([skinObjTitle isKindOfClass:[UIColor class]])
            {
                lab.textColor = skinObjTitle;
            }
            
            if([skinObjBG isKindOfClass:[UIColor class]])
            {
                lab.backgroundColor = skinObjBG;
            }
        }
        else if ([self.targetObj isKindOfClass:[UISegmentedControl class]])
        {
            UISegmentedControl * segment = (UISegmentedControl *)self.targetObj;
            if([skinObjBG isKindOfClass:[UIColor class]])
            {
                segment.tintColor = skinObjBG;
            }
        }
        
        
        
        //放到最后
        else if ([self.targetObj isKindOfClass:[UIView class]])
        {
            UIView * lab = (UIView *)self.targetObj;
            
            if([skinObjBG isKindOfClass:[UIColor class]])
            {
                lab.backgroundColor = skinObjBG;
            }
        }
    }
    
    
    -(void)dealloc
    {
        //    NSLog(@"释放皮肤变化视图的监听");
        
        self.targetObj = nil;
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:SKIN_COLOR_CHANGE_NOTIFICATION object:nil];
    }
    
    
    @end
    
    
    
    #pragma mark - SkinFontStyleModule -
    
    @implementation SkinFontStyleModule
    
    -(instancetype)initWithTarget:(id)targetObj andNormalFontSize:(NSInteger)normalSize
    {
        self = [super init];
        if(self)
        {
            self.targetObj = targetObj;
            self.fontNormal = normalSize;
            
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(skinFontStyleChange) name:SKIN_FONT_STYLE_NOTIFICATION object:nil];
            
            [self skinFontStyleChange];
        }
        
        return self;
    }
    
    - (void)skinFontStyleChange
    {
        if([self.targetObj isKindOfClass:[UILabel class]])
        {
            UILabel * lab = (UILabel *)self.targetObj;
    
            lab.font = [UIFont bbxSystemFont:self.fontNormal];
        }
        else if ([self.targetObj isKindOfClass:[UIButton class]])
        {
            UIButton * btn = (UIButton *)self.targetObj;
            
            btn.titleLabel.font = [UIFont bbxSystemFont:self.fontNormal];
        }
        else if ([self.targetObj isKindOfClass:[UISegmentedControl class]])
        {
            UISegmentedControl * segment = (UISegmentedControl *)self.targetObj;
            [segment setTitleTextAttributes:@{NSFontAttributeName:[UIFont bbxSystemFont:14]} forState:UIControlStateNormal];
        }
        else if ([self.targetObj isKindOfClass:[UINavigationBar class]])
        {
            UINavigationBar * barV = (UINavigationBar *)self.targetObj;
            [barV setTitleTextAttributes:@{NSFontAttributeName:[UIFont bbxSystemFont:self.fontNormal],NSForegroundColorAttributeName:[UIColor whiteColor]}];
        }
        
    }
    
    
    
    -(void)dealloc
    {
        self.targetObj = nil;
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:SKIN_FONT_STYLE_NOTIFICATION object:nil];
    }
    
    
    @end
    

    其中UIFont+BBX.h是对字体的特殊设置,我是这样子写的

    
    + (UIFont *)defaultButtonFont
    {
        if([SettingManager shareManager].appFontName)
        {
            return [self bbxSystemFont:14 withFontName:[SettingManager shareManager].appFontName];
        }
        else
        {
            return [UIFont systemFontOfSize:[self fontSize:14]];
        }
    }
    
    +(UIFont *)bbxSystemFont:(float)size
    {
        if([SettingManager shareManager].appFontName)
        {
            return [self bbxSystemFont:size withFontName:[SettingManager shareManager].appFontName];
        }
        else
        {
            return [UIFont systemFontOfSize:[self fontSize:size]];
        }
    }
    
    +(UIFont *)bbxBoldSystemFont:(float)size
    {
        if([SettingManager shareManager].appFontName)
        {
            return [self bbxSystemFont:size withFontName:[SettingManager shareManager].appFontName];
        }
        else
        {
            return [UIFont boldSystemFontOfSize:[self fontSize:size]];
        }
    }
    
    
    +(UIFont *)bbxSystemFont:(float)size withFontName:(NSString *)fontName
    {
        return [UIFont fontWithName:fontName size:[self fontSize:size]];
    }
    
    + (CGFloat)fontSize:(CGFloat)oldFont
    {
        switch ([SettingManager shareManager].appFont) {
            case FontSize_Normal:
                oldFont = oldFont;
                break;
            case FontSize_Small:
                oldFont = oldFont-1;
                break;
            case FontSize_Smallest:
                oldFont = oldFont-2;
                break;
            case FontSize_Big:
                oldFont = oldFont+2;
                break;
            case FontSize_Biger:
                oldFont = oldFont+4;
                break;
            case FontSize_Bigest:
                oldFont = oldFont+6;
                break;
                
            default:
                break;
        }
        
        if([MacroAppInfo isiPhone6P])
        {
            return oldFont;
        }
        return oldFont;
    }
    

    可以自己控制和扩展。

    皮肤设置

    运用

    比如控制vc的背景颜色变化

    self.view.skinColorStyle = [[SkinColorStyleModule alloc] initWithTarget:self.view andBlock:^(SkinColorStyleModule *obj) {
                obj.skinBgKey = kSkinType_bg;
            }];
    

    设置title的文字颜色和背景变化

    titlePop.skinColorStyle = [[SkinColorStyleModule alloc] initWithTarget:titlePop andBlock:^(SkinColorStyleModule *obj) {
            obj.skinTitleKey = kSkinType_title_segment_home;
            obj.skinBgKey = kSkinType_bg;
        }];
    

    设置字体大小样式变化

    self.userName.skinFontStyle = [[SkinFontStyleModule alloc] initWithTarget:self.userName andNormalFontSize:14];
    

    好用你就拍拍手

    APP-锁事(加密记事本)

    这个app中运用了,欢迎大家支持.

    相关文章

      网友评论

      • 阿兹尔:能上传个Demo
      • 8bef82b761ae:还是习惯runtime模式
        T_aa:@梦儿飘 看个人习惯吧,我觉得我自己这个比较简单,也好理解。
        所有的视图都集中在一起做处理。不是很复杂。只是在实例的时候需要休息一下。看使用习惯:smiley:
        8bef82b761ae:@T_aa 对,把所有的UIKit,CAlayer等都加上。你肯定觉得特麻烦,但是好处在于APP如果字体颜色很多很乱,你这种方法也跟麻烦。言而总之,就是改这个很麻烦( ̄~ ̄)。安卓容易很多:fearful::fearful:
        T_aa:@梦儿飘 runtime 是不是每个控件都要去加相应的模式属性。

      本文标题:iOS夜间模式,换肤,换字体样式

      本文链接:https://www.haomeiwen.com/subject/acxxjttx.html