UIBarButtonItem外观iOS5字体?

时间:2012-07-19 21:01:38

标签: iphone objective-c ios uibarbuttonitem

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

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

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

这似乎适用于iOS 7.1:

[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],
                                 NSFontAttributeName:[UIFont fontWithName:@"Resamitz" size:16.0]}
                                            forState:UIControlStateNormal];