UITabBarItem

UITabBarItem

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];
    }
    
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容