更改UITabBarItem中的字体

31 iphone xcode uitabbarcontroller ios

嗨,我有这个代码,它不起作用,我做错了什么?

- (void)viewDidLoad
{    
    [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont, nil] forState:UIControlStateDisabled];
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,这不是我的viewDidLoad中唯一的东西,但我只是想告诉你那些我把它放在哪里.

Chr*_*hey 67

根据:如何在iOS 5中更改UITabBarItem中的文本颜色

看起来解决方案可能是将消息发送到外观代理,而不是一个项目:

(在iOS 7.0+中已弃用)

[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont: [UIFont fontWithName:@"AmericanTypewriter" size:20.0f]} forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

对于iOS 7.0+,请使用:

[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"AmericanTypewriter" size:20.0f]} forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

  • 改变forstate:UIControlStateNormal (4认同)
  • 这里没有明确提到.您可以将此代码放在app delegate中的didFinishLaunchingWithOptions函数中,以便为app设置它 (3认同)
  • 适用于iOS7 +的NSFontAttributeName和适用于iOS6的UITextAttributeFont (2认同)

Wuj*_*ujo 11

快速的方式,懒惰:

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .selected)
Run Code Online (Sandbox Code Playgroud)

  • 对于`.selected`状态,这不会改变我的字体. (3认同)
  • @AbbasAngouti 用于更改字体处于选中状态,您应该使用以下内容:/sf/answers/3327093871/ (2认同)

小智 5

雨燕3

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "OpenSans", size: 10)!], for: .normal)
Run Code Online (Sandbox Code Playgroud)


Mac*_*wia 5

Swift 4.1和自定义字体

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Montserrat-Medium", size: 11)], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Montserrat-Medium", size: 11)], for: .selected)
Run Code Online (Sandbox Code Playgroud)