Sunday, June 9, 2013

UINavigationBar titleTextAttributes example in Objective C (iOS).


UINavigationBar titleTextAttributes

Display attributes for the bar’s title text.

@property(nonatomic, copy) NSDictionary *titleTextAttributes

Discussion of [UINavigationBar titleTextAttributes]
You can specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary, using the text attribute keys described in NSString UIKit Additions Reference.

UINavigationBar titleTextAttributes example.
In iOS 5 you can change the navigationBar title color in this manner:

navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor yellowColor] forKey:UITextAttributeTextColor];

Example of [UINavigationBar titleTextAttributes].
I do believe proper way to set the colour of UINavigationBar is:

NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],UITextAttributeTextColor, nil];
self.titleTextAttributes = attributes;

UINavigationBar titleTextAttributes example.
// 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],
}];

End of UINavigationBar titleTextAttributes example article.