Search code examples
iphoneiosxcode4

How can I change color of UITabBarItem's title when it's selecetd


My env,

iOS6
Xcode 4.5.1

I want to change a color of UITabBarItem's title when it's selected.

I use CustomUITabBarItem for UITabBarItem. - customUITabBarItem.m

@implementation customUITabBarItem

@synthesize customHighlightedImage;

-(UIImage *) selectedImage
{
    return self.customHighlightedImage;
}

- (void) dealloc
{
    [customHighlightedImage release];
    customHighlightedImage=nil;
    [super dealloc];
}

@end

ViewController.m

#import "FirstViewController.h"
#import "customUITabBarItem.h"


@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad
{   
    [super viewDidLoad];
    CustomUITabBarItem *tabItem = [[customUITabBarItem alloc] initWithTitle:@"first" image:[UIImage imageNamed:@"first.png"] tag:0]; 
    tabItem.customHighlightedImage = [UIImage imageNamed:@"first_selected.png"];
    self.tabBarItem = tabItem;
    [tabItem release];
    tabItem = nil;
}   

How can I change the color?


Solution

  •  [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                [UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont,
                                                [UIColor blackColor], UITextAttributeTextColor,
                                                [UIColor grayColor],        
                                                nil]];
    

    And please ensure that this only works for iOS 5.0 or later.