Change the font of the title bar navigation - fast

I have a title in my navigation bar and I want to change it to a custom font. I found this line of code, but this is when you have a navigation controller.

self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "LeagueGothic-Regular", size: 16.0)!, NSForegroundColorAttributeName: UIColor.whiteColor()] 

But I do not have a navigation controller. I added the navigation bar manually for viewing.

enter image description here

enter image description here

How can I change the font of the comment?

+28
source share
7 answers

Try this:

Objective-c

 [[UINavigationBar appearance] setTitleTextAttributes:attrsDictionary]; 

Swift 3

 self.navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "CaviarDreams", size: 20)!] 

Swift 4

 self.navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "CaviarDreams", size: 20)!] 
+57
source

The correct way to set the font for each view controller in Swift (using the Appearance proxy):

Swift 4

 let attributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 17)!] UINavigationBar.appearance().titleTextAttributes = attributes 

Swift 3

 let attributes = [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 17)!] UINavigationBar.appearance().titleTextAttributes = attributes 
+26
source

SWIFT 4.x

To change the font of the navigation bar title for the regular and large title above iOS 11.x

 let navigation = UINavigationBar.appearance() let navigationFont = UIFont(name: "Custom_Font_Name", size: 20) let navigationLargeFont = UIFont(name: "Custom_Font_Name", size: 34) //34 is Large Title size by default navigation.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: navigationFont!] if #available(iOS 11, *){ navigation.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: navigationLargeFont!] } 

A large title should be installed on the navigation bar.

+10
source

You can also do this in a storyboard, in Xcode 10.1 there is a bug that does this here to solve this problem.

Step 1 - Choose a system from the font

enter image description here

Step 2 - Then select Custom again and it will show all the fonts.

enter image description here

+7
source
  self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Lato-Semibold", size: 17)!,NSAttributedStringKey.foregroundColor : UIColor.white] 
0
source

Swift 4.2 Xcode 10

 self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Sacramento-Regular", size: 19)!] 
0
source

To call titleTextAttributes in a link to a navigation bar, use:

 let attributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 17)!] self.navigationController?.navigationBar.titleTextAttributes = attributes 
-one
source

All Articles