How to change the Color of text in UITabBarItem in iOS 5
Asked Answered
W

6

27

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];
Wheelchair answered 7/12, 2011 at 7:53 Comment(1)
I
38

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 }];
Interrogative answered 7/12, 2011 at 8:1 Comment(12)
@Wheelchair It can't be set in iOS4 as far as I know. However, you can customize your tabBar to do so. You can try THIS. It works excellent. ;)Interrogative
hi kjuly, thanks for the quick reply. i'm setting this in my app delegate with error.Wheelchair
[[UITabBarItem appearance] 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]];Wheelchair
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIBarItemAppearance setTitleTextAttributes:]: unrecognized selector sent to instance 0x82773d0'Wheelchair
@Wheelchair what's your build target? :? Does [self.tabBarItem setTitleTextAttributes: work?Interrogative
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Rokkitt" size:26.0], UITextAttributeFont, nil]]; workedWheelchair
for my nav bar, build target iOS 5.0Wheelchair
@Wheelchair you're welcome! BTW, what cause your new error? I'm wondering. :pInterrogative
not too sure, i retype it instead of copying, add in forState:UIControlStateNormal tooWheelchair
by the way how do i remove dropshadow and gradient from the tabbar icons ?Wheelchair
@Wheelchair Maybe you can set tintColor for it just like for navigationBar. I've not tried yet. :pInterrogative
@Wheelchair HERE's an interesting post.Interrogative
M
13
[[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];
Mcghee answered 4/6, 2013 at 15:58 Comment(1)
1 up for simple and effective answerAltercate
H
10

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
Harberd answered 31/7, 2013 at 12:2 Comment(0)
A
4

Specifically for iOS 7, try using NSForegroundColorAttributeName instead of UITextAttributeTextColor

Amblyoscope answered 24/9, 2013 at 21:16 Comment(0)
G
1

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

I have had the same problem and searched for the past hour and finally realized that my issue is because I didn't put the code into method viewWillAppear. Not sure if this is common sense as I just started with objective-c but thought this should be another piece of important information to the answer as the same code didn't work inside viewDidLoad.

According to this post, this code only works if put in the method viewWillAppear.

Giselagiselbert answered 6/6, 2014 at 0:54 Comment(0)
K
1

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];
}
Kuroshio answered 27/11, 2014 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.