2012-07-19 45 views
0

我正在使用下面的一小段代码来改变我的导航栏标题应用程序的文本属性,它的效果很好。UIBarButtonItem外观iOS5字体?

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

回答

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]; 
+0

嗯,没想到这一点。试了一下,它编译和运行,这是进步,但它并没有改变我的酒吧按钮文本的任何属性!我已经在我的OP中添加了使用的代码。 – 2012-07-19 21:21:31

+0

@JoshKahane尝试我刚刚发布的内容 – Nosrettap 2012-07-19 21:38:46

0

这似乎在iOS 7.1的工作:

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