我在添加到导航控制器的 BarButtonItem 的 swift Xcode 项目中更改字体时遇到问题。我能够毫无问题地更改按钮的颜色,但字体不会改变。法典:
var navTextColor = UIColor(red:0.3, green:0.09, blue:0.05, alpha:1.0)
self.navigationController?.navigationBar.tintColor = navTextColor
如果您创建并输出(例如 @IBOutlet var barButton: UIBarButtonItem!
( 链接到您的UIBarButtonItem
,您应该能够通过使用插座上的setTitleTextAttributes
来更改字体类型。
barButton.setTitleTextAttributes([ NSFontAttributeName: UIFont(name: "Arial", size: 12)!], forState: UIControlState.Normal)
雨燕3
barButton.setTitleTextAttributes([ NSFontAttributeName: UIFont(name: "Arial", size: 12)!], for: UIControlState.normal)
Swift 3
更改所有TabBarItem
字体的另一种简单方法是在UITabBarController
ViewDidLoad()
使用此代码:(无需创建插座(
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "IranSansMobile", size: 15)!], for: UIControlState.normal)
Swift 5
barButton.setTitleTextAttributes([ NSAttributedString.Key.font: UIFont(name: "Arial", size: 12)!], for: UIControl.State.normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "Arial", size: 12)!], for: .normal)