Search code examples
iosswiftnavigationbar

change navigation bar title font - swift


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

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

But i don't have any navigation controller. I added navigation bar manually to my view.

enter image description here

enter image description here

how can i change comment font?


Solution

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