我的环境

iOS6
Xcode 4.5.1


我想在选择UITabBarItem标题时更改其颜色。

我对UITabBarItem使用CustomUITabBarItem。
-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;
}


如何更改颜色?

最佳答案

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


并且请确保这仅适用于iOS 5.0或更高版本。

10-08 01:44