如何在iOS 5中更改UITabBarItem中的文本颜色

在iOS 5中有更多的外观控制,我们如何更改UITabBarItem文本颜色? 从默认白色到其他颜色?

编辑:工作解决scheme

[[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]; 

你的意思是这个吗? 请记住,这只适用于iOS5.0或更高版本。

 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]]; } 

苹果有关定制外观的文档:

在iOS v5.0及更高版本中,可以使用UIBarItem声明的外观select器通过设置项目标签文本属性来自定义标签栏的外观。 您还可以使用“自定义外观”中列出的方法。您可以使用外观代理(例如[UITabBarItem外观])自定义所有分段控件的外观,或者只使用单个选项卡栏。 您还可以使用“pipe理完成的选定图像”中列出的方法提供完成的选定和未select的图像; 但是,这些方法不参与UIAppearance代理API(请参阅UIAppearance)。 UIKit现在可以提供任何自动处理完成的图像。 为了获得良好的效果,您必须使用setFinishedSelectedImage:withFinishedUnselectedImage:来提供完成的选定和未select的图像。

编辑:这是另一个使用UIAppearance系统和NSDictionary字面语法的例子:

 [[UITabBarItem appearance] setTitleTextAttributes:@{ UITextAttributeFont : [UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeTextColor : [UIColor blackColor], UITextAttributeTextShadowColor : [UIColor grayColor], UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)]}]; 

编辑 (by @JeremyWiebe):从iOS 6开始,字典键已被更改为与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 }]; 
 [[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]; 

UITextAttributeFont,UITextAttributeTextColor等在iOS 7.0中被弃用。

你必须使用:

 NSFontAttributeName, NSParagraphStyleAttributeName, NSForegroundColorAttributeName, NSBackgroundColorAttributeName, NSLigatureAttributeName, NSKernAttributeName, NSStrikethroughStyleAttributeName, NSUnderlineStyleAttributeName, NSStrokeColorAttributeName, NSStrokeWidthAttributeName, NSShadowAttributeName and NSVerticalGlyphFormAttributeName 

特别是对于iOS 7,请尝试使用NSForegroundColorAttributeName而不是UITextAttributeTextColor

适用于iOS 7.0+的工作解决scheme:

 - (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]; } 

我没有足够的声望点来添加评论,所以我会在此添加另一个答案。

我有同样的问题,并search了过去一小时,终于意识到我的问题是因为我没有把代码放在方法viewWillAppear 。 不知道这是否是常识,因为我刚开始使用objective-c,但认为这应该是另一个重要信息的答案,因为相同的代码无法在viewDidLoad内工作。

根据这个post ,这个代码只有在viewWillAppear方法中才起作用。