如何在 iOS 5 中更改 UITabBarItem 中文本的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8412010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the Color of text in UITabBarItem in iOS 5
提问by Desmond
with more appearance control in iOS 5, how do we change the UITabBarItem text color ? from default white to other color ?
在 iOS 5 中有更多的外观控制,我们如何更改 UITabBarItem 文本颜色?从默认白色到其他颜色?
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];
回答by Kjuly
Do you mean this one? Keep in mind, this only works for iOS5.0 or later.
你是说这个吗?请记住,这仅适用于 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]];
}
Apple's documentation on customizing appearance:
Apple 关于自定义外观的文档:
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:.
在 iOS v5.0 及更高版本中,您可以通过使用 UIBarItem 声明的外观选择器设置项目标签文本属性来自定义标签栏的外观。您也可以使用“自定义外观”中列出的方法。您可以使用外观代理(例如,[UITabBarItem 外观])或仅单个标签栏来自定义所有分段控件的外观。您还可以使用“管理完成的选定图像”中列出的方法提供完成的选定和未选定的图像;但是,这些方法不参与 UIAppearance 代理 API(请参阅 UIAppearance)。UIKit 现在确实为完成的图像提供任何自动处理。为了获得好的结果,您必须使用 setFinishedSelectedImage:withFinishedUnselectedImage: 以匹配对的形式提供已完成的选择和未选择的图像。
Edit: Here is another example using the UIAppearancesystem and the NSDictionaryliteral syntax:
编辑:这是另一个使用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)]}];
Edit(by @JeremyWiebe): As of iOS 6, the dictionary keys have been changed to be the same as OS X uses:
编辑(@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 }];
回答by carmen_munich
[[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];
回答by schirrmacher
UITextAttributeFont, UITextAttributeTextColor etc. are deprecated in iOS 7.0.
UITextAttributeFont、UITextAttributeTextColor 等在 iOS 7.0 中已弃用。
You have to use:
你必须使用:
NSFontAttributeName, NSParagraphStyleAttributeName, NSForegroundColorAttributeName, NSBackgroundColorAttributeName, NSLigatureAttributeName, NSKernAttributeName, NSStrikethroughStyleAttributeName, NSUnderlineStyleAttributeName, NSStrokeColorAttributeName, NSStrokeWidthAttributeName, NSShadowAttributeName and NSVerticalGlyphFormAttributeName
回答by seanoshea
Specifically for iOS 7, try using NSForegroundColorAttributeName instead of UITextAttributeTextColor
专门针对 iOS 7,尝试使用 NSForegroundColorAttributeName 而不是 UITextAttributeTextColor
回答by Jove Kuang
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.
我遇到了同样的问题并搜索了过去一个小时,终于意识到我的问题是因为我没有将代码放入方法viewWillAppear 中。不确定这是否是常识,因为我刚开始使用 Objective-c,但认为这应该是答案的另一条重要信息,因为相同的代码在 viewDidLoad 中不起作用。
According to this post, this code only works if put in the method viewWillAppear.
根据这篇文章,此代码仅在放入方法 viewWillAppear 时才有效。
回答by voghDev
Working solution for iOS 7.0+:
适用于 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];
}