更改所选UITabBarItem的字体与目标c

问题描述:

我想将所选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://*.com/a/41082581/4831524 –

+0

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

+0

@ liangju对于UIStateNormal我的解决方案已经工作,但没有选择。 –

试试这个:

// 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

这可悲也只影响选定的颜色.. –

试过这样:

// 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

这就像我的解决方案。 –

[[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上的字体参数不适用于选定状态。 –