使用 objective c 更改所选 UITabBarItem 的字体

Change font for selected UITabBarItem with objective c

我想将选中的 UITabBarItem 的字体更改为粗体(如果选中)。我按照以下步骤将图像和文本设置为白色,还设置了字体,但只有选择 UITabBarItem 时颜色会发生变化,字体不会发生变化。

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

所选字体没有任何变化。

日历文本应为粗体。

试试这个:

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

试过这个:

    // 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];
  [[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];

如@TimurBernikowich 所述,字体参数 不适用于 iOS11 上的选定状态