How to change text color in UITabBarItem in iOS 5

With more control over appearance in iOS 5, how can we change the text color of UITabBarItem? from default white to a different 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]; 
+27
ios iphone xcode uitabbarcontroller uitabbar
Dec 07 2018-11-11T00:
source share
6 answers

Do you mean this? Keep in mind that 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 Documentation for Customizing the Appearance:

In iOS version 5.0 and later, you can customize the appearance of tab bars by setting the attributes of the text labels of the elements using the view switches declared by UIBarItem. You can also use the methods listed in the Customizing the Appearance section. You can customize the appearance of all segmented controls using an appearance proxy (for example, [Appearance UITabBarItem]) or just one tab bar. You can also provide ready-made selected and unselected images using the methods listed in the "Managing the finished selected image" section; these methods, however, do not participate in the UIAppearance proxy API (see UIAppearance). UIKit now provides automatic treatment of finished images. For good results, you must provide ready-made selected and unselected images in matching pairs using setFinishedSelectedImage: withFinishedUnselectedImage :.

Edit: Here is another example of using the UIAppearance system and 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): Starting with iOS 6, dictionary keys have been changed the same way as in OS X:

 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 }]; 
+38
Dec 07 '11 at 8:01
source share
 [[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]; 
+12
Jun 04 '13 at 15:58
source share

UITextAttributeFont, UITextAttributeTextColor etc. Deprecated in iOS 7.0.

You should use:

 NSFontAttributeName, NSParagraphStyleAttributeName, NSForegroundColorAttributeName, NSBackgroundColorAttributeName, NSLigatureAttributeName, NSKernAttributeName, NSStrikethroughStyleAttributeName, NSUnderlineStyleAttributeName, NSStrokeColorAttributeName, NSStrokeWidthAttributeName, NSShadowAttributeName and NSVerticalGlyphFormAttributeName 
+10
Jul 31 '13 at 12:02
source share

In particular, for iOS 7, try using NSForegroundColorAttributeName instead of UITextAttributeTextColor

+4
Sep 24 '13 at 21:16
source share

Working solution for iOS 7.0 +:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal]; [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected]; } 
+1
Nov 27 '14 at 16:21
source share

I do not have enough reputation points to add a comment, so I will add another answer.

I had the same problem, and I searched for the last hour, and finally realized that my problem was that I did not put the code in the viewWillAppear method. Not sure if this is common sense since I was just starting out with objective-c, but thought it should be another piece of important information for the answer, since the same code did not work inside viewDidLoad.

According to this post , this code only works if it is placed in the viewWillAppear method.

0
Jun 06
source share



All Articles