Alex Stone
Alex Stone

Reputation: 47348

iPhone UINavigationBar change font style for all controllers with [UINavigationBar appearance]

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!

Upvotes: 6

Views: 10902

Answers (3)

runmad
runmad

Reputation: 14886

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.

Upvotes: 6

Ross
Ross

Reputation: 14415

@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!

Upvotes: 19

TigerCoding
TigerCoding

Reputation: 8720

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

Upvotes: 23

Related Questions