28

Can I change the font of my UINavigationController? --> title

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Christian 'fuzi' Orgler
  • 1,682
  • 8
  • 27
  • 51

5 Answers5

74

As of iOS 5 you can change the font via the appearance proxy.

https://developer.apple.com/documentation/uikit/uiappearance

The following will set the title font for all UINavigationControllers.

  NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
  [titleBarAttributes setValue:[UIFont fontWithName:@"Didot" size:16] forKey:NSFontAttributeName];
  [[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];

To set the font for the back button, do this:

  NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary: [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateNormal]];
  [attributes setValue:[UIFont fontWithName:@"Didot" size:12] forKey:NSFontAttributeName];
  [[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];

To set the font for the large titles available in iOS 11+, do this:

if (@available(iOS 11.0, *)) {
    NSMutableDictionary *largeTitleTextAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] largeTitleTextAttributes]];
    [largeTitleTextAttributes setValue:[UIFont fontWithName:@"Didot" size:32] forKey:NSFontAttributeName];
    [[UINavigationBar appearance] setLargeTitleTextAttributes:largeTitleTextAttributes];
}
Samuel De Backer
  • 3,404
  • 2
  • 26
  • 37
morgancodes
  • 25,055
  • 38
  • 135
  • 187
  • 4
    iOS 7 deprecated the key: UITextAttributeFont (read, don't use this) and now uses the key: NSFontAttributeName instead. Works wonders and makes my app look a heck of a lot better. Thanks! – Rob R. Oct 03 '13 at 16:30
  • 1
    In each individual view controller you can also set the title text attributes on the instance of current navigation bar to customize the appearance there, like this code snippet: [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Palatino-Bold" size:28], NSForegroundColorAttributeName:[UIColor whiteColor]}]; – CodeBrew Mar 21 '15 at 16:53
26

for iOS8+ you can use:

[self.navigationController.navigationBar setTitleTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"MyFont" size:18.0f],
                                                                   NSForegroundColorAttributeName: [UIColor whiteColor]
                                                                   }];

Swift:

self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "MyFont", size: 18.0)!]
Mobile Developer
  • 5,730
  • 1
  • 39
  • 45
  • 4
    Thanks this gave me the right direction to implement a custom font with swift: self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "MyFont", size: 18.0)!] – NBoymanns Mar 15 '16 at 08:10
  • Thanks, mate! Solved it. – Felipe Apr 14 '16 at 17:58
  • 2
    Always sad when trying out first all above answers just to find out the bottom one is the most modern one \: – hashier Nov 30 '16 at 12:29
15

The title view can be any view. So just create a UILabel or something else where you change the font and assign that new view to the title property of the navigation item.

Erik
  • 5,791
  • 5
  • 30
  • 45
10

An example:

-(void) viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    CGRect frame = CGRectMake(0, 0, 400, 44);
    UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
    label.backgroundColor = [UIColor clearColor];
    label.font = [FontHelper fontFor:FontTargetForNavigationHeadings];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text = self.navigationItem.title;
    // emboss in the same way as the native title
    [label setShadowColor:[UIColor darkGrayColor]];
    [label setShadowOffset:CGSizeMake(0, -0.5)];
    self.navigationItem.titleView = label;
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
Jonathan Moffatt
  • 13,309
  • 8
  • 51
  • 49
  • +1 for this. If viewControllers in stack require different font for title, then `[UINavigationBar appearance]` can't do the job. A custom titleView is the only method. – BabyPanda Oct 19 '12 at 04:15
  • 5
    its better to put this code in viewDidLoad, else every time the view comes up in hierarchy, titleView will be created and its unnecessary work on the system – raw3d Oct 06 '13 at 13:30
3

The answer from @morgancodes will set the font for all UINavigationController titles. I've updated it for Swift 4:

let attributes = [NSAttributedStringKey.font: UIFont(name: "Menlo", size: 14) as Any]
UINavigationBar.appearance().titleTextAttributes = attributes
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, for: .normal)
apb
  • 3,270
  • 3
  • 29
  • 23