【问题标题】:How to change the Color of text in UITabBarItem in iOS 5如何在 iOS 5 中更改 UITabBarItem 中文本的颜色
【发布时间】:2012-01-14 18:27:39
【问题描述】:

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

【问题讨论】:

标签: iphone ios xcode uitabbarcontroller uitabbar


【解决方案1】:

你是说这个吗?请记住,这仅适用于 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 外观])自定义所有分段控件的外观,或者仅自定义单个选项卡栏的外观。您还可以使用“管理完成的选定图像”中列出的方法提供完成的选定图像和未选定图像;但是,这些方法不参与 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)]}];

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

【讨论】:

  • @Desmond 据我所知无法在 iOS4 中设置。但是,您可以自定义 tabBar 来执行此操作。你可以试试THIS。它工作得很好。 ;)
  • 嗨 kjuly,感谢您的快速回复。我在我的应用委托中设置此错误。
  • [[UITabBarItem 外观] 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]];
  • 不太确定,我重新输入而不是复制,也添加 forState:UIControlStateNormal
  • @Desmond HERE 的帖子很有趣。
【解决方案2】:
[[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];

【讨论】:

  • 1 up 以获得简单有效的答案
【解决方案3】:

UITextAttributeFont、UITextAttributeTextColor 等在 iOS 7.0 中已弃用。

你必须使用:

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

【讨论】:

    【解决方案4】:

    专门针对 iOS 7,尝试使用 NSForegroundColorAttributeName 而不是 UITextAttributeTextColor

    【讨论】:

      【解决方案5】:

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

      我也遇到了同样的问题,搜索了一个小时,终于意识到我的问题是因为我没有将代码放入方法viewWillAppear中。不确定这是否是常识,因为我刚开始使用 Objective-c,但认为这应该是答案的另一条重要信息,因为相同的代码在 viewDidLoad 中不起作用。

      根据this post,此代码仅在放入方法 viewWillAppear 时才有效。

      【讨论】:

        【解决方案6】:

        适用于 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];
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-03-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-24
          • 2023-01-24
          相关资源
          最近更新 更多