Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set font & color of the title in UINavigationBar using iOS5 appearance API?

I have a multiple View Controllers and I want to set the font color of all to red.

 [[UINavigationBar appearance] setFont:[UIFont boldSystemFontOfSize:12.0]]; 

is throwing an unrecognized selector error.

How can I fix this?

like image 999
carbonr Avatar asked May 03 '12 11:05

carbonr


People also ask

How do you set a font in Word?

Open any Word document. Right-click somewhere in the document and choose “Font”. In the Font dialog box, select your preferred typeface and any other settings you want to change (e.g., font size). Click the “Set As Default” button.


2 Answers

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

Or if you prefer with the object literal style:

[[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, -1)],     UITextAttributeFont: [UIFont fontWithName:@"Arial-Bold" size:0.0], }]; 

Edit for iOS 7 and following

UITextAttributes are deprecate as iOS 7 you can use the following :

NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowColor = [UIColor colorWithWhite:.0f alpha:1.f]; shadow.shadowOffset = CGSizeMake(0, -1);  [[UINavigationBar appearance] setTitleTextAttributes:@{      NSForegroundColorAttributeName: [UIColor whiteColor],      NSShadowAttributeName: shadow,      NSFontAttributeName: [UIFont fontWithName:@"Arial-Bold" size:15.0f]      }]; 
like image 90
TigerCoding Avatar answered Sep 28 '22 08:09

TigerCoding


For deployment targets greater than or equal to iOS 6, you should use NSShadow instead:

NSShadow * shadow = [[NSShadow alloc] init]; shadow.shadowColor = [UIColor lightGrayColor]; shadow.shadowOffset = CGSizeMake(0, -2);  NSDictionary * navBarTitleTextAttributes = @{ NSForegroundColorAttributeName : [UIColor redColor],    NSShadowAttributeName          : shadow,    NSFontAttributeName            : [UIFont systemFontOfSize:14] };  [[UINavigationBar appearance] setTitleTextAttributes:navBarTitleTextAttributes]; 

enter image description here

like image 44
Robert Avatar answered Sep 28 '22 06:09

Robert