0

我正在使用如下所示的一小段代码来更改导航栏标题应用程序的文本属性,并且效果很好。

[[UINavigationBar appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [UIColor whiteColor], UITextAttributeTextColor,
      [UIColor grayColor], UITextAttributeTextShadowColor,
      [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
      [UIFont fontWithName:@"Cochin-BoldItalic" size:0.0], UITextAttributeFont,
      nil]];

但我也希望能够轻松地为 UIBarButtonItem 文本执行此操作,但我无法弄清楚,因为它不共享它出现的相同或相似的方法。

编辑

尝试了这段代码,没有对文本进行任何更改:

[[UIBarItem appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [UIColor whiteColor], UITextAttributeTextColor,
      [UIColor grayColor], UITextAttributeTextShadowColor,
      [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
      [UIFont fontWithName:@"Cochin-BoldItalic" size:12.0], UITextAttributeFont,
      nil]
     forState:UIControlStateNormal];
4

2 回答 2

10

您想使用 UIBarItem 的- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state方法(UIBarButtonItem 继承自 UIBarItem)。

查看文档了解更多详情:http: //developer.apple.com/library/ios/#documentation/uikit/reference/UIBarItem_Class/Reference/Reference.html#//apple_ref/occ/cl/UIBarItem

试试这个:

//Suppose you have initialized barButton elsewhere`
[barButton setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [UIColor whiteColor], UITextAttributeTextColor,
      [UIColor grayColor], UITextAttributeTextShadowColor,
      [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
      [UIFont fontWithName:@"Cochin-BoldItalic" size:12.0], UITextAttributeFont,
      nil]
     forState:UIControlStateNormal];
于 2012-07-19T21:13:13.150 回答
0

这似乎适用于 iOS 7.1:

[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],
                                 NSFontAttributeName:[UIFont fontWithName:@"Resamitz" size:16.0]}
                                            forState:UIControlStateNormal];
于 2014-04-29T08:41:42.930 回答