Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

with more appearance control in iOS 5, how do we change the UITabBarItem text color ? from default white to other color ?

EDIT:working solution

  [[UITabBarItem appearance] setTitleTextAttributes:
         [NSDictionary dictionaryWithObjectsAndKeys:
          [UIColor blackColor], UITextAttributeTextColor, 
          [UIColor whiteColor], UITextAttributeTextShadowColor, 
          [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, 
          [UIFont fontWithName:@"Rok" size:0.0], UITextAttributeFont, 
          nil] 
                                              forState:UIControlStateNormal];
like image 709
Desmond Avatar asked Dec 07 '11 07:12

Desmond


People also ask

How do I change the color of text in tab bar?

How to change tab bar text color (selected/unselected) In tab bar, there are two states of the text. One is when tab is selected and another one is when the tab is unselected. To change the text color for selected state, inside the TabBar widget, add the labelColor property and set the color.

How do I add an image to the tab bar in iOS?

iOS UITabBarController Changing Tab Bar Item Title and Icon For a custom icon, add the required images to the assets folder and set the 'System Item' from earlier to 'custom'. Now, set the icon to be shown when the tab is selected from the 'selected image' drop down and the default tab icon from the 'image' drop down.


4 Answers

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 }]; 
like image 188
Kjuly Avatar answered Oct 08 '22 15:10

Kjuly


[[UITabBarItem appearance] setTitleTextAttributes:@{                              UITextAttributeFont : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],                         UITextAttributeTextColor : [UIColor colorWithRed:0/255.0 green:48/255.0 blue:92/255.0 alpha:1.0],}                                          forState:UIControlStateNormal];  [[UITabBarItem appearance] setTitleTextAttributes:@{                              UITextAttributeFont : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],                         UITextAttributeTextColor : [UIColor colorWithRed:0/255.0 green:138/255.0 blue:196/255.0 alpha:1.0],}                                          forState:UIControlStateSelected]; 
like image 31
carmen Avatar answered Oct 08 '22 14:10

carmen


UITextAttributeFont, UITextAttributeTextColor etc. are deprecated in iOS 7.0.

You have to use:

NSFontAttributeName, NSParagraphStyleAttributeName, NSForegroundColorAttributeName, NSBackgroundColorAttributeName, NSLigatureAttributeName, NSKernAttributeName, NSStrikethroughStyleAttributeName, NSUnderlineStyleAttributeName, NSStrokeColorAttributeName,  NSStrokeWidthAttributeName, NSShadowAttributeName and NSVerticalGlyphFormAttributeName
like image 27
schirrmacher Avatar answered Oct 08 '22 13:10

schirrmacher


Specifically for iOS 7, try using NSForegroundColorAttributeName instead of UITextAttributeTextColor

like image 30
seanoshea Avatar answered Oct 08 '22 14:10

seanoshea