Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change navigationBar font in Swift?

This is what I have tried so far, but receiving an error (I have correctly implemented CaviarDreams to the project):

self.navigationController.navigationBar.titleTextAttributes = NSFontAttributeName[UIFont .fontWithName(CaviarDreams.ttf, size: 20)] 

Error says: Use of unresolved identifier 'CaviarDreams

like image 319
b3rge Avatar asked Aug 19 '14 16:08

b3rge


People also ask

How do I change the navigation title font in Swift?

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.

How do I change the font in SwiftUI?

Hold the command key and click the text to bring up a pop-over menu. Choose Show SwiftUI Inspector and then you can edit the text/font properties.


1 Answers

Try this:

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

Edit: Now, UIFont must be unwrapped to be able to be used here.

Swift 5 (+ safe handling of optional UIFont)

self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedString.Key.font: UIFont(name: "Caviar-Dreams", size: 20) ?? UIFont.systemFont(ofSize: 20)] 
like image 109
Jim T Avatar answered Sep 25 '22 02:09

Jim T