UITabBarItem
- 父類是UIBarItem
- UITabBarController(標(biāo)簽控制器)的UITabBar(標(biāo)簽條)的模型,設(shè)置了它,底層會(huì)根據(jù)這個(gè)模型設(shè)置UITabBar的UITabBarButton的內(nèi)容。
UITabBarItem
- UITabBarItem有以下屬性影響著UITabBarButton的內(nèi)容
// 標(biāo)題文字 nav.tabBarItem.title = title; // 圖標(biāo) nav.tabBarItem.image = (imageName.length)?[UIImage imageNamed:imageName]:nil; // 選中時(shí)的圖標(biāo) //IOS7之后,默認(rèn)按鈕被選中,則圖片會(huì)被渲染成藍(lán)色;以前的不會(huì) nav.tabBarItem.selectedImage = (selectedImageName.length)?[UIImage imageNamed:selectedImageName]:nil; // 提醒數(shù)字 // 若有圖標(biāo),則提醒數(shù)字在圖標(biāo)右方,否則在左方;數(shù)字最多建議為99+ nav.tabBarItem.badgeValue = @"99+";
設(shè)置TabBarItem的文字屬性
文字屬性的設(shè)置詳見富文本
-
方案一:直接設(shè)置每一個(gè)tabBarItem對(duì)象(不推薦)
// 普通狀態(tài)下的文字屬性 NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary]; normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14]; normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor]; [vc.tabBarItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal]; // 選中狀態(tài)下的文字屬性 NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary]; selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor]; [vc.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected]; // 字典中用到的key 1.iOS7之前(在UIStringDrawing.h中可以找到) - 比如UITextAttributeFont\UITextAttributeTextColor - 規(guī)律:UITextAttributeXXX 2.iOS7開始(在NSAttributedString.h中可以找到) - 比如NSFontAttributeName\NSForegroundColorAttributeName - 規(guī)律:NSXXXAttributeName
-
方案二:通過(guò)UITabBarItem的appearance對(duì)象統(tǒng)一設(shè)置(推薦)
#pragma mark - initilize + (void)initialize { [super initialize]; // 判斷是否是該類本身,子類也返回 if(self != [BSTabBarController class])return; // 獲取tabBarItem外觀 UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil]; // 普通狀態(tài) NSMutableDictionary *normalDict = [NSMutableDictionary dictionary]; normalDict[NSFontAttributeName] = [UIFont systemFontOfSize:titleFont];//字體 normalDict[NSForegroundColorAttributeName] = [UIColor grayColor];//顏色 [item setTitleTextAttributes:normalDict forState:UIControlStateNormal]; // 選中狀態(tài) NSMutableDictionary *selectedDict = [NSMutableDictionary dictionary]; selectedDict[NSForegroundColorAttributeName] = [UIColor darkGrayColor]; [item setTitleTextAttributes:selectedDict forState:UIControlStateSelected]; }