在iOS 5中有更多的外观控制,我们如何改变UITabBarItem文本的颜色?从默认的白色到其他颜色?
编辑:工作解决方案
[[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]];
}
Apple关于定制外观的文档:
在iOS v5.0及更高版本中,你可以通过使用UIBarItem声明的外观选择器来设置项目标签文本属性来自定义选项卡栏的外观。您还可以使用"自定义外观"中列出的方法。您可以使用外观代理自定义所有分段控件的外观(例如,[UITabBarItem appearance]),也可以只自定义单个选项卡栏的外观。您还可以使用"管理已完成的选定图像"中列出的方法提供已完成的选定图像和未选中图像;这些方法,虽然,不参与UIAppearance代理API(参见UIAppearance)。UIKit现在不提供任何自动处理完成的图像。为了获得良好的效果,您必须使用setfinisheselecteimage: withfinishedunselecteimage:.
提供已完成的选中和未选中的图像。
编辑:下面是另一个使用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
我没有足够的声望积分来添加评论,所以我将在这里添加另一个答案。
我也遇到了同样的问题,在过去的一个小时里搜索,最后意识到我的问题是因为我没有把代码放入方法viewWillAppear。不确定这是否是常识,因为我刚开始与objective-c,但认为这应该是另一块重要的信息,以回答相同的代码没有工作在viewDidLoad
根据这篇文章,这段代码只工作,如果放在方法viewWillAppear。
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];
}