如何在iOS 5中更改UITabBarItem中的文本颜色 [英] How to change the Color of text in UITabBarItem in iOS 5

查看:76
本文介绍了如何在iOS 5中更改UITabBarItem中的文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

with more appearance control in iOS 5, how do we change the UITabBarItem text color ? from default white to other color ?

编辑:工作解决方案

  [[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或更高版本。

Do you mean this one? Keep in mind, this only works for iOS5.0 or later.

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关于自定义外观的文档:

Apple's documentation on customizing appearance:


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

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:.

编辑:
以下是使用 UIAppearance 系统和 NSDictionary 文字语法的另一个示例:

Here is another example using the UIAppearance system and the NSDictionary literal syntax:

[[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使用的相同:


Edit (by @JeremyWiebe): As of iOS 6, the dictionary keys have been changed to be the same as OS X uses:

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

这篇关于如何在iOS 5中更改UITabBarItem中的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆