2017-05-03 57 views
0

我想將所選UITabBarItem的字體更改爲粗體,如果它被選中。我已經做了以下設置圖像和文字爲白色,我也設置字體,但只有顏色更改選擇UITabBarItem,而不是字體。更改所選UITabBarItem的字體與目標c

// Normal font  
[self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:MY_LIGHT_FONT, NSFontAttributeName, [UIColor grayColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal]; 

// Selected font 
[self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:MY_BOLD_FONT, NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName,nil] forState:UIControlStateSelected]; 

所選字體不會改變任何內容。

enter image description here

日曆的文字應該大膽。

+0

檢查這個答案http://stackoverflow.com/a/41082581/4831524 –

+0

看此http:/ /stackoverflow.com/questions/11069437/changing-font-in-uitabbaritem – liangju

+0

@ liangju對於UIStateNormal我的解決方案已經工作,但沒有選擇。 –

回答

0

試試這個:

// Normal font  
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:MY_LIGHT_FONT, NSFontAttributeName, [UIColor grayColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal]; 

// Selected font 
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:MY_BOLD_FONT, NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName,nil] forState:UIControlStateSelected]; 
+0

這可悲也隻影響選定的顏色.. –

0

試過這樣:

// Normal font 
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"your-font" size:10.f], NSFontAttributeName, nil] forState:UIControlStateNormal]; 

    //selected state 
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"your-font bold" size:10.f], NSFontAttributeName, nil] forState:UIControlStateSelected]; 
+0

這就像我的解決方案。 –

2
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:MONSREGULAR size:10.0f], 
                 NSForegroundColorAttributeName :[UIColor colorWithRed:0.004 green:0.820 blue:0.369 alpha:1.00]; 
                 } forState:UIControlStateSelected]; 


    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:MONSREGULAR size:10.0f], 
                 NSForegroundColorAttributeName :[UIColor darkgraycolor]; 
                 } forState:UIControlStateNormal]; 
+0

iOS11上的字體參數不適用於選定狀態。 –