ios 更改 UITabBarItem 中的字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11069437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing font in UITabBarItem
提问by Chris Trahey
Hi I have this code and it doesn't work, what am I doing wrong?
嗨,我有这个代码,但它不起作用,我做错了什么?
- (void)viewDidLoad
{
[self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont, nil] forState:UIControlStateDisabled];
}
BTW that's not the only thing in my viewDidLoad but I just wanted to show you guys thats where I put it.
顺便说一句,这不是我 viewDidLoad 中唯一的东西,但我只是想向你们展示我把它放在哪里。
回答by Chris Trahey
As per: How to change the Color of text in UITabBarItem in iOS 5
根据:如何在 iOS 5 中更改 UITabBarItem 中的文本颜色
It looks like the solution may be sending the message to the appearance proxy, instead of one item:
看起来解决方案可能是将消息发送到外观代理,而不是一项:
(Deprecated in iOS 7.0+)
(在 iOS 7.0+ 中已弃用)
[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont: [UIFont fontWithName:@"AmericanTypewriter" size:20.0f]} forState:UIControlStateNormal];
For iOS 7.0+ use:
对于 iOS 7.0+ 使用:
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"AmericanTypewriter" size:20.0f]} forState:UIControlStateNormal];
回答by Wujo
Swift way, for lazies:
快速方式,懒人:
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .selected)
回答by Maciej
Swift 4.1 and custom font
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)
回答by Artur
Swift 3
斯威夫特 3
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "OpenSans", size: 10)!], for: .normal)
回答by niggeulimann
Swift 4
斯威夫特 4
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.tabbar], for: .normal)