使用 Swift (Xcode 6) 更改 BarButtonItem 字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27570826/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:23:31  来源:igfitidea点击:

Change BarButtonItem Font Using Swift (Xcode 6)

xcodeswiftfontsnavigationuinavigationbar

提问by Seth

I am having trouble changing the font in my swift Xcode project of a BarButtonItem that was added to a navigation controller. I was able to change the color of the button without a problem, but the font will not change. Code:

我无法更改已添加到导航控制器的 BarButtonItem 的 Swift Xcode 项目中的字体。我可以毫无问题地更改按钮的颜色,但字体不会改变。代码:

var navTextColor = UIColor(red:0.3, green:0.09, blue:0.05, alpha:1.0)
self.navigationController?.navigationBar.tintColor = navTextColor

回答by Jér?me

If you create and outlet (e.g. @IBOutlet var barButton: UIBarButtonItem!) linked to your UIBarButtonItem, you should be able to change your font type by using setTitleTextAttributeson the outlet.

如果您创建并将插座(例如@IBOutlet var barButton: UIBarButtonItem!)链接到您的UIBarButtonItem,您应该能够通过setTitleTextAttributes在插座上使用来更改您的字体类型。

barButton.setTitleTextAttributes([ NSFontAttributeName: UIFont(name: "Arial", size: 12)!], forState: UIControlState.Normal)

Swift3

斯威夫特3

barButton.setTitleTextAttributes([ NSFontAttributeName: UIFont(name: "Arial", size: 12)!], for: UIControlState.normal)

回答by Milad Faridnia

Swift 3

斯威夫特 3

Another easy way to change all TabBarItems font is using this code in ViewDidLoad()of UITabBarController: (No need to create an Outlet )

另一种简单的方法来改变所有TabBarItem的字体在使用此代码ViewDidLoad()UITabBarController:(无需创建一个插座)

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "IranSansMobile", size: 15)!], for: UIControlState.normal)