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

Des*_*ond 27 iphone xcode uitabbarcontroller uitabbar ios

在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];
Run Code Online (Sandbox Code Playgroud)

Kju*_*uly 38

你是说这个吗?请记住,这仅适用于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]];
}
Run Code Online (Sandbox Code Playgroud)

Apple关于自定义外观的文档:

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

编辑:这是使用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)]}];
Run Code Online (Sandbox Code Playgroud)

编辑(由@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 }];
Run Code Online (Sandbox Code Playgroud)

  • @Desmond据我所知,它不能在iOS4中设置.但是,您可以自定义tabBar来执行此操作.您可以尝试[THIS](https://github.com/boctor/idev-recipes/tree/master/CustomTabBar).它非常棒.;) (5认同)
  • @Desmond [HERE](http://praxisstalled.blogspot.com/2010/08/customising-appearance-of.html)是一篇有趣的帖子. (5认同)
  • 不太确定,我重新键入它而不是复制,添加forState:UIControlStateNormal (2认同)

car*_*ich 13

[[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];
Run Code Online (Sandbox Code Playgroud)


sch*_*her 10

UITextAttributeFont,UITextAttributeTextColor等在iOS 7.0中已弃用.

你必须使用:

NSFontAttributeName, NSParagraphStyleAttributeName, NSForegroundColorAttributeName, NSBackgroundColorAttributeName, NSLigatureAttributeName, NSKernAttributeName, NSStrikethroughStyleAttributeName, NSUnderlineStyleAttributeName, NSStrokeColorAttributeName,  NSStrokeWidthAttributeName, NSShadowAttributeName and NSVerticalGlyphFormAttributeName
Run Code Online (Sandbox Code Playgroud)