How do i change navigationBar font in Swift?

b3rge picture b3rge · Aug 19, 2014 · Viewed 42.9k times · Source

How do I change the 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

Sorry if the question is extremely bad.

Answer

Jim T picture Jim T · Aug 19, 2014

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)]