Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change UINavigationItem font?

I am trying to change font property for UINavigationItem.

I've tried using titleTextAttributes but somehow I am only able to change title font.

How can I change the font or UINavigationItem ? For example, the "More" text for the Back UIButton shown below:

enter image description here

I've tried using:

self.navigationController?.navigationBar.titleTextAttributes = [
            NSForegroundColorAttributeName: UIColor.blackColor(),
            NSFontAttributeName: UIFont(name: "Papyrus", size: 18)!
        ]

but it only changes what is shown on the picture, not the "More" Button's Font.

like image 701
Fengson Avatar asked Mar 01 '15 16:03

Fengson


1 Answers

The reason your method wasn't working is because you were just using titleTextAttributes = ... when you have to use setTitleTextAttributes:forState: I use this function to customize the Nav Bar globally for my entire project:

func customizeNavBar(_ color: UIColor, titleFont: UIFont, buttonFont: UIFont) {

    UINavigationBar.appearance().tintColor = color
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: color, NSFontAttributeName: titleFont]
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: color, NSFontAttributeName: buttonFont], forState: UIControlState.Normal)
}

For a single instance, call the same functions:

someBarButton.tintColor.setTitleTextAttributes([NSForegroundColorAttributeName: color, NSFontAttributeName: buttonFont], forState: UIControlState.Normal)
like image 147
Ian Avatar answered Sep 23 '22 15:09

Ian