How to Use Settitletextattributes:Forstate in Uibaritem

How do you use setTitleTextAttributes:forState in UIBarItem?

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

Error when instantiating a UIFont in an text attributes dictionary

The initializer of UIFont returns an optional because it may fail due to misspelled font name etc.

You have to unwrap it and check:

if let font = UIFont(name: "AvenirNext", size: 15) {
barButton.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
}

UPDATED for Swift 3

if let font = UIFont(name: "AvenirNext", size: 15) {
barButton.setTitleTextAttributes([NSFontAttributeName:font], for: .normal)
}

[_UIBarItemAppearance setTitleColor:forState:]: unrecognized selector sent to instance

It's because it doesn't exist in the class UIBarButtonItem or in it's super class

You can use this method define in UIBarItem

 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.
Availability
Available in iOS 5.0 and later.

See Also

– titleTextAttributesForState:  

Declared In

UIBarItem.h  

UIBarButtonItem appearance setTitleTextAttributes does not affects UIControlStateDisabled state

You have to set it for both control state Normal and Disabled.

(2015-11-18 -- As of iOS 9.1 you must still set both.)

setTitleTextAttributes doesn't work for UITabBarItem when it is unselected in swift

Here is the code that works in this case to put still in the UITabBarController:

override func viewDidLoad() {
super.viewDidLoad()

//custom tab bar
self.tabBar.barTintColor = UIColor(red: 0.0/255.0, green: 102.0/255.0, blue: 153.0/255.0, alpha: 1)
self.tabBar.tintColor = UIColor(red: 171.0/255.0, green: 203.0/255.0, blue: 61.0/255.0, alpha: 1)

for item in self.tabBar.items as [UITabBarItem]
{
item.image = item.selectedImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Normal)
item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Disabled)
item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red: 171.0/255.0, green: 203.0/255.0, blue: 61.0/255.0, alpha: 1)], forState:UIControlState.Selected)
}
}

How to change the Color of text in UITabBarItem in iOS 5

Do you mean this one? Keep in mind, this only works for iOS5.0 or later.

if ([self.tabBarItem respondsToSelector:@selector(setTitleTextAttributes:)]) {
NSLog(@"*** Support method(iOS 5): setTitleTextAttributes:");
[self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont,
[UIColor blackColor], UITextAttributeTextColor,
[UIColor grayColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], UITextAttributeTextShadowOffset,
nil]];
}

Apple's documentation on customizing appearance:

In iOS v5.0 and later, you can customize the appearance of tab bars by setting item label text attributes using appearance selectors declared by UIBarItem. You can also use the methods listed in “Customizing Appearance.” You can customize the appearance of all segmented controls using the appearance proxy (for example, [UITabBarItem appearance]), or just of a single tab bar. You can also provide finished selected and unselected images using the methods listed in “Managing the Finished Selected Image”; these methods, though, do not participate in the UIAppearance proxy API (see UIAppearance). UIKit does now provide any automatic treatment to finished images. For good results, you must provide finished selected and unselected images in matching pairs using setFinishedSelectedImage:withFinishedUnselectedImage:.

Edit:
Here is another example using the UIAppearance system and the NSDictionary literal syntax:

[[UITabBarItem appearance] setTitleTextAttributes:@{
UITextAttributeFont : [UIFont fontWithName:@"AmericanTypewriter" size:20.0f],
UITextAttributeTextColor : [UIColor blackColor],
UITextAttributeTextShadowColor : [UIColor grayColor],
UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)]}];

Edit (by @JeremyWiebe):
As of iOS 6, the dictionary keys have been changed to be the same as OS X uses:

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor grayColor];
shadow.shadowOffset = CGSizeMake(0, 1.0);

[[UITabBarItem appearance] setTitleTextAttributes:@{
NSFontAttributeName : [UIFont fontWithName:@"AmericanTypewriter" size:20.0f],
NSForegroundColorAttributeName : [UIColor blackColor],
NSShadowAttributeName : shadow }];


Related Topics



Leave a reply



Submit