Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change BarButtonItem Font Using Swift (Xcode 6)

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
like image 877
Seth Avatar asked Dec 19 '14 17:12

Seth


Video Answer


2 Answers

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)
like image 64
Jérôme Avatar answered Sep 21 '22 08:09

Jérôme


Swift 3

Another easy way to change all TabBarItems 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)
like image 24
Milad Faridnia Avatar answered Sep 18 '22 08:09

Milad Faridnia