64

The question is plain easy and simple, the answer unfortunately not.

How can you change the font of the text in the UINavigationBar?

Macarse
  • 91,829
  • 44
  • 175
  • 230
Joetjah
  • 6,292
  • 8
  • 55
  • 90
  • 2
    Just because this is the first result I got when I went googling for the subject, and there is a more updated answer: http://stackoverflow.com/a/10440362/700471 – Matt Mc Oct 12 '12 at 17:59
  • I do believe there is a difference in both questions. Where I'm asking to change the font of an individual navigation bar, the other question is about changing the font of 'all' navigation bars. Even though, it's a usefull link to mention as others might be interested! – Joetjah Oct 22 '12 at 12:07
  • 7
    For individual navigation bar, use this: `[self.navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeFont:[UIFont fontWithName:<#(NSString *)#> size:<#(CGFloat)#>]}];` – Philip007 Nov 09 '12 at 11:56
  • 1
    For iOS7 you shall use NSFontAttributeName instead of UITextAttributeFont – ishahak May 02 '14 at 05:09

7 Answers7

158

From iOS 7 and later:

NSShadow* shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(0.0f, 1.0f);
shadow.shadowColor = [UIColor redColor];
[[UINavigationBar appearance] setTitleTextAttributes: @{
     NSForegroundColorAttributeName: [UIColor greenColor],
                NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20.0f],
              NSShadowAttributeName: shadow
                                                      }];

From iOS 5 and later:

 [[UINavigationBar appearance] setTitleTextAttributes: @{
                                UITextAttributeTextColor: [UIColor greenColor],
                          UITextAttributeTextShadowColor: [UIColor redColor],
                         UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
                                     UITextAttributeFont: [UIFont fontWithName:@"Helvetica" size:20.0f]
     }];

Earlier than iOS 5:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;  
self.navigationItem.titleView = label;      
[label release];
Anton Gaenko
  • 8,929
  • 6
  • 44
  • 39
Aravindhan
  • 15,608
  • 10
  • 56
  • 71
15

If you wanted to change the font in the Interface Builder itself (without any code) here is the way to do it in Xcode6:

1.) Find the Navigation Bar view under the Navigation Controller Scene enter image description here

2.) Change the Title Font, Color and Shadow attributes in the Attributes Inspector. enter image description here

inder
  • 943
  • 11
  • 15
5

The above answer works. I would add the following line before the last line. If I don't, it seems the label is center-aligned incorrectly if there is a back button on the left side but no right button.

...
[self.navigationItem.titleView sizeToFit]; 
[label release]; // not needed if you are using ARC
Raj Lalwani
  • 83
  • 1
  • 5
5

Updated for iOS 7:

[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                      [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
                                                      shadow, NSShadowAttributeName,
                                                      [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];

courtesy of:

http://www.appcoda.com/customize-navigation-status-bar-ios-7/

AbuZubair
  • 1,236
  • 14
  • 20
4

Not sure why all the answers included the shadow. Adding the lines that manipulate the shadow does nothing in respect to changing text font. These 2 lines of code will work for iOS 8.4 and Swift

let attributesDictionary = [NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 14)!]
navigationController!.navigationBar.titleTextAttributes = attributesDictionary

The titleTextAttributes stores a dictionary that will dictate the font, color, size, and other attributes of the navigation bar's title.

Kelvin Lau
  • 6,373
  • 6
  • 34
  • 57
  • And the default color of the text will be whatever the `UINavigationBar`'s `tintColor` is. If you want something different, set the tint color, or add an `NSForegroundColorAttributeName` attribute and value to that attributes dictionary. – NRitH Aug 24 '15 at 00:44
  • How to let this use the SystemFont? – Brabbeldas Nov 01 '15 at 16:53
1

As of iOS 5 you can use the appearance proxy.

The answer is in a duplicate of this question: https://stackoverflow.com/a/12364740/883413

Community
  • 1
  • 1
Ric Santos
  • 15,419
  • 6
  • 50
  • 75
1
     NSShadow *shadow = [NSShadow new];
[shadow setShadowColor: [UIColor clearColor]];
[shadow setShadowOffset: CGSizeMake(0.0f, 1.0f)];

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                         [UIFont fontWithName:@"TimeBurner" size:27.0f], NSFontAttributeName,
                                         [UIColor whiteColor], NSForegroundColorAttributeName,
                                         shadow, NSShadowAttributeName,nil]];
Vin
  • 456
  • 6
  • 18
  • I have downloaded the font named timeburner_regular.ttf so I wrote the above code. It worked for me. – Vin Mar 06 '14 at 14:06