Sunday, June 9, 2013

UINavigationBar setTitleTextAttributes example in Objective C (iOS).


UINavigationBar setTitleTextAttributes

Display attributes for the bar’s title text.

@property(nonatomic, copy) NSDictionary *titleTextAttributes

Discussion of [UINavigationBar setTitleTextAttributes]
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 setTitleTextAttributes 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 setTitleTextAttributes].
I do believe proper way to set the colour of UINavigationBar is:

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

UINavigationBar setTitleTextAttributes 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 setTitleTextAttributes example article.