Search code examples
text-alignmentuiappearance

SettingUINavigationbar textAlignment with UIappearance possible?


I just wondered if it is possible to globally change the UINavigationBar's title textAlignment like you can do with its font:

NSMutableDictionary *titleBarAttributes     = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
[titleBarAttributes setValue:[UIFont fontWithName:myFontName] forKey:NSFontAttributeName];
[[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];

Does something like [titleBarAttributes setValue:NSTextAlignmentLeft forKey:NSTextAlignment] exist for UIAppereance?


Solution

  • I am afraid the answer is no. You can only have the following keys for the text attributes from the NSString UIKit Additions Reference:

    NSString *const UITextAttributeFont; 
    NSString *const UITextAttributeTextColor; 
    NSString *const UITextAttributeTextShadowColor; 
    NSString *const UITextAttributeTextShadowOffset;
    

    But you can still set the titleView property of the UINavigationItem with UILabel and you can set the text alignment there.

    You can create a base subclass of view controller, set the navigation item titleView property, and create subsequent view controllers by subclassing that base view controller.