Change font of UINavigationController title

Christian 'fuzi' Orgler picture Christian 'fuzi' Orgler · Oct 30, 2010 · Viewed 29.7k times · Source

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

Answer

morgancodes picture morgancodes · Aug 10, 2012

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];
}