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:
var navTextColor = UIColor(red:0.3, green:0.09, blue:0.05, alpha:1.0)
self.navigationController?.navigationBar.tintColor = navTextColor
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 setTitleTextAttributes
on the outlet.
barButton.setTitleTextAttributes([ NSFontAttributeName: UIFont(name: "Arial", size: 12)!], forState: UIControlState.Normal)
Swift3
barButton.setTitleTextAttributes([ NSFontAttributeName: UIFont(name: "Arial", size: 12)!], for: UIControlState.normal)
Swift 3
Another easy way to change all TabBarItem
s font is using this code in ViewDidLoad()
of UITabBarController
: (No need to create an Outlet )
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "IranSansMobile", size: 15)!], for: UIControlState.normal)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With