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

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

最佳答案

您要使用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];

关于iphone - UIBarButtonItem外观iOS5字体?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11569230/

10-13 09:37