How to change UINavigationItem font?
Asked Answered
L

4

9

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.

Levo answered 1/3, 2015 at 16:7 Comment(0)
S
13

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)
Sansom answered 1/3, 2015 at 16:13 Comment(5)
This changes font for all buttons, I need to change it for only one in specific class / Navigation ControllerLevo
Then instead of UINavigationBar.appearance() you call the same functions on an instance of UINavigationBar (Presumably navigationController.navigationBar) answer updatedSansom
I've tried that already, but I'm getting an error "UINavigationItem does not have a mamber named setTitleTextAttributes. Nor does navigationBar... I can only use the one I've presented in my initial post.. :< Edit: Check my initial post - I've added a code I'm using.Levo
Nevermind, I was using NavigationItem instead of BarItem inside :) It's working now, thanks!Levo
I need to just change the navigation title color and title font, i dont have any buttons on navigation item. Where to apply this function and how to use it 'Charbonnier
B
0

Swift 4

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal)
Bourgeois answered 17/1, 2018 at 16:10 Comment(0)
O
0

For iOS 13 and Swift 5.

For setting the bar button item text color & font:

UIBarButtonItem.appearance().setTitleTextAttributes([
    .foregroundColor: UIColor.white, 
    .font: UIFont(name: GetFontNameBold(), size: 40)! ],
    for: UIControl.State.normal)

For setting the title color & font just add in viewDidLoad() the following line:

UINavigationBar.appearance().titleTextAttributes = [
    .foregroundColor: UIColor.white, 
    .font: UIFont(name: "Arial", size: 24)! ]
Obstruent answered 10/4, 2020 at 7:54 Comment(0)
C
0

if you have outlet do this:

titleItem.titleLabel.font = UIFont(name: NAME, size: SIZE)
Chromatism answered 27/5, 2020 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.