• [BS-03] 统一设置UITabBarController管理的所有VC的tabBarItem图标文字的颜色大小等属性


    1.

    统一设置UITabBarController管理的所有VC的tabBarItem图标文字的颜色大小等属性

      
    1.  统一设置UITabBarController管理的所有VC的tabBarItem图标文字的颜色大小等属性
    
    //设置字体大小及颜色(可变字典才可用[]方法设值)
        NSMutableDictionary *attr = [NSMutableDictionary dictionary];
        attr[NSFontAttributeName] = [UIFont systemFontOfSize:19];
        attr[NSForegroundColorAttributeName] = [UIColor grayColor];
        
        NSMutableDictionary *selectAttr = [NSMutableDictionary dictionary];
        selectAttr[NSFontAttributeName] = [UIFont systemFontOfSize:5];//NSFontAttributeName是个const变量,一旦设置了19,后面就不能再改了,这里改为5无效
        selectAttr[NSForegroundColorAttributeName] = [UIColor darkGrayColor];//不能写成NSBackgroundColorAttributeName
        
        UITabBarItem *item = [UITabBarItem appearance];
        [item setTitleTextAttributes:attr forState:UIControlStateNormal];
        [item setTitleTextAttributes:selectAttr forState:UIControlStateSelected];
    
    
    2. 就不用像下面这样单独给每一个VC.tabBarItem设置字体大小和颜色了
    [vc01.tabBarItem setTitleTextAttributes:attr forState:UIControlStateNormal]; [vc01.tabBarItem setTitleTextAttributes:selectAttr forState:UIControlStateSelected]; [vc02.tabBarItem setTitleTextAttributes:attr forState:UIControlStateNormal]; [vc02.tabBarItem setTitleTextAttributes:selectAttr forState:UIControlStatelectSelected]; [vc03.tabBarItem setTitleTextAttributes:attr forState:UIControlStateNormal]; [vc03.tabBarItem setTitleTextAttributes:selectAttr forState:UIControlStateSelected];

    2. 

    原理是:setTitleTextAttributes:forState: 函数后面有备注UI_APPEARANCE_SELECTOR,

    可以通过 [Class appearance] setTitleTextAttributes: forState: ]; 来同一设置字体颜色。

    //设置UITabBarItem:UIBarItem的颜色属性
    
    NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarItem : NSObject <NSCoding, UIAppearance>
    
    - (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
    
    - (nullable NSDictionary<NSString *,id> *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR

    3. 

    封装了如下方法,后续要同一设置根控制器UITabBarController管理的各个VC的tabBarItem的字体和颜色,可直接在UITabBarController的viewDidLoad方法中调用[self  setTabBarItemFontAndColor];

    //统一设置所有vc的TabBarItem的字体和颜色
    - (void)setTabBarItemFontAndColor {
    
        //设置字体大小及颜色(可变字典才可用此方法)
        NSMutableDictionary *attr = [NSMutableDictionary dictionary];
        attr[NSFontAttributeName] = [UIFont systemFontOfSize:15];
        attr[NSForegroundColorAttributeName] = [UIColor grayColor];//NSBackgroundColorAttributeName字体的背景颜色,一般不需要
        
        NSMutableDictionary *selectAttr = [NSMutableDictionary dictionary];
        selectAttr[NSFontAttributeName] = [UIFont systemFontOfSize:5];//NSFontAttributeName是个const变量,一旦设置了19,后面就不能再改了,这里改为5无效
        selectAttr[NSForegroundColorAttributeName] = [UIColor darkGrayColor];//这里不能写成NSBackgroundColorAttributeName
        
        UITabBarItem *item = [UITabBarItem appearance];
        [item setTitleTextAttributes:attr forState:UIControlStateNormal];
        [item setTitleTextAttributes:selectAttr forState:UIControlStateSelected];//注意此处为UIControlStateSelected,不能写成Highlighted了
    
        //原理是:setTitleTextAttributes:forState: 函数后面有备注UI_APPEARANCE_SELECTOR,可以通过 [Class appearance] setTitleTextAttributes: forState: ]; 来同一设置字体颜色。
    }
    iOS开发者交流群:180080550
  • 相关阅读:
    三角函数
    第十七次作业
    第十六次作业
    第15次作业
    第13次java作业
    第十二次java作业
    第十一次java作业
    第十次java作业
    第九次java
    第八次java作业
  • 原文地址:https://www.cnblogs.com/stevenwuzheng/p/5416432.html
Copyright © 2020-2023  润新知