6

I'm aware that I can individually change the font of a navigation bar as outlined in this answer: Change the navigation bar's font

Currently I'm using a more global approach:

//in my app delegate:
    [[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];

Is there a way to globally change the font that the Navbar through the appearance object?

thank you!

Community
  • 1
  • 1
Alex Stone
  • 46,408
  • 55
  • 231
  • 407
  • 1
    http://stackoverflow.com/questions/8412010/how-to-chage-the-color-of-text-in-uitabbaritem-in-ios-5 Similar one. :p – Kjuly Dec 08 '11 at 05:01
  • BTW, keep in mind, it only works for iOS5.0. – Kjuly Dec 08 '11 at 05:03
  • So long as you aren't changing nav bars (pushing doesn't change the nav bar, just the view), then applying the font in one class, applies it to the whole bar. However, if using multiple bars, dearchiving a value from an NSUserDefault might not be such a bad idea. (in fact, now that I think about it, registering a default might be the only sure way). – CodaFi Dec 08 '11 at 05:07

3 Answers3

24

From Ray Wenderlich:

http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

// Customize the title text for *all* UINavigationBars
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
    [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], 
    UITextAttributeTextColor, 
    [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
    UITextAttributeTextShadowColor, 
    [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
    UITextAttributeTextShadowOffset, 
    [UIFont fontWithName:@"Arial-Bold" size:0.0], 
    UITextAttributeFont, 
    nil]];
TigerCoding
  • 8,710
  • 10
  • 47
  • 72
20

@Javy's answer with @Philip007's suggestion:

[[UINavigationBar appearance] setTitleTextAttributes: @{
                            UITextAttributeTextColor: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
                      UITextAttributeTextShadowColor: [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
                     UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
                                 UITextAttributeFont: [UIFont fontWithName:@"Helvetica-Light" size:0.0f]
 }];

ahh... that's better!

runmad
  • 14,846
  • 9
  • 99
  • 140
Ross
  • 14,266
  • 12
  • 60
  • 91
6

Above answers with updates for deprecated keys and use of NSShadow:

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowBlurRadius = 0.0;
shadow.shadowOffset = CGSizeMake(0.0, 2.0);
[[UINavigationBar appearance] setTitleTextAttributes: @{
                     NSForegroundColorAttributeName : [UIColor blackColor],
                                NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Light" size:0.0f],
                              NSShadowAttributeName : shadow
}];

Also setting the font size to 0 so it automatically resizes based on navigation bar orientation/height.

runmad
  • 14,846
  • 9
  • 99
  • 140