Search code examples
iosobjective-cswiftuinavigationcontrolleruibaritem

How do you use setTitleTextAttributes:forState in UIBarItem?


How do you use setTitleTextAttributes:forState: in UIBarItem in iOS?

How do you set the NSDictionary? Can't make it work and documentation isn't very clear about that.

From the documentation:

setTitleTextAttributes:forState:

Sets the title’s text attributes for a given control state:

- (void)setTitleTextAttributes:(NSDictionary *)attributes 
                      forState:(UIControlState)state

Parameters:

attributes: A dictionary containing key-value pairs for text attributes. You can specify the font, text color, text shadow color, and text shadow offset using the keys listed in NSString UIKit Additions Reference.

state: The control state for which you want to set the text attributes for the title.


Solution

  • Example code:

    [[UIBarItem appearance] setTitleTextAttributes:
    [NSDictionary dictionaryWithObjectsAndKeys:
    [UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0], UITextAttributeTextColor, 
    [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], UITextAttributeTextShadowColor, 
    [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, 
    [UIFont fontWithName:@"AmericanTypewriter" size:0.0], UITextAttributeFont, nil] 
    forState:UIControlStateNormal];