码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-04-21 13:38:13      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:

统一设置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[NSBackgroundColorAttributeName] = [UIColor darkGrayColor];
    
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:attr forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectAttr forState:UIControlStateHighlighted];


2. 就不用像下面这样单独给每一个VC.tabBarItem设置字体大小和颜色了
[vc01.tabBarItem setTitleTextAttributes:attr forState:UIControlStateNormal]; [vc01.tabBarItem setTitleTextAttributes:selectAttr forState:UIControlStateHighlighted]; [vc02.tabBarItem setTitleTextAttributes:attr forState:UIControlStateNormal]; [vc02.tabBarItem setTitleTextAttributes:selectAttr forState:UIControlStateHighlighted]; [vc03.tabBarItem setTitleTextAttributes:attr forState:UIControlStateNormal]; [vc03.tabBarItem setTitleTextAttributes:selectAttr forState:UIControlStateHighlighted];

 

原理是:

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

 

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

标签:

原文地址:http://www.cnblogs.com/stevenwuzheng/p/5416432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!