Reputation: 385
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
Upvotes: 9
Views: 9231
Reputation: 3
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "Arial", size: 12)!], for: .normal)
Upvotes: 0
Reputation: 1360
Swift 5
barButton.setTitleTextAttributes([ NSAttributedString.Key.font: UIFont(name: "Arial", size: 12)!], for: UIControl.State.normal)
Upvotes: 1
Reputation: 9477
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)
Upvotes: 2
Reputation: 8066
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)
Upvotes: 23