Seth
Seth

Reputation: 385

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

Upvotes: 9

Views: 9231

Answers (4)

SEmih Karabulut
SEmih Karabulut

Reputation: 3

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "Arial", size: 12)!], for: .normal)

Upvotes: 0

Carl Smith
Carl Smith

Reputation: 1360

Swift 5

barButton.setTitleTextAttributes([ NSAttributedString.Key.font: UIFont(name: "Arial", size: 12)!], for: UIControl.State.normal)

Upvotes: 1

Milad Faridnia
Milad Faridnia

Reputation: 9477

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)

Upvotes: 2

Jérôme
Jérôme

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

Related Questions