2014-02-06 49 views
9

我試圖更改我的UIBarButtonItem字體。 ViewControllers加載時看起來不錯。但是,如果我點擊條形按鈕,或者向右滑動就好像移動到前一個ViewController(但是然後拉回到當前的那個),字體將變回系統字體。這是我在我的AppDelegate我設置:iOS 7 UIBarButtonItem字體在點擊時更改

NSDictionary* barButtonItemAttributes = @{NSFontAttributeName: [UIFont fontWithName:@"SourceSansPro-Light" size:20.0f]}; 
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateNormal]; 
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateHighlighted]; 
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateSelected]; 
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateDisabled]; 

這是我的viewWillAppear中的一個例子:

- (void) viewWillAppear:(BOOL)animated { 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed)]; 
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0]; 
} 

難道我莫名其妙地改變字體回,還是我濫用外觀代理?

回答

15

問題是tintColor設置與標題文本屬性衝突。評論這條線,一切都會很好。

如果你要使用的標題文本屬性,然後合併文本顏色爲這些屬性:

NSDictionary* barButtonItemAttributes = 
@{NSFontAttributeName: 
     [UIFont fontWithName:@"Georgia" size:20.0f], 
    NSForegroundColorAttributeName: 
     [UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0] 
}; 
+0

感謝。這似乎是真的。但是我怎樣才能改變每個ViewController的基礎上的按鈕顏色? – hodgesmr

+0

在iOS 7中,NSAttributedString是第一類對象,因此顏色現在是文本屬性。因此,不要使用外觀代理,並與個人'tintColor'發生衝突,只需製作按鈕併爲其文本提供所需的屬性,包括字體和顏色。 – matt

+0

我編輯過來說明我的意思。 – matt

3

通過使用viewDidLoad方法,您可以設置自定義文字,字體,大小等一次性這種方法地點。

UIView *customBarButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)]; 

UIButton *buttonDone = [UIButton buttonWithType:UIButtonTypeSystem]; 
[buttonDone addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

buttonDone.frame = CGRectMake(10, 0, 50, 30); 

buttonDone.titleLabel.textColor = [UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0]; 
buttonDone.titleLabel.font = [UIFont fontWithName:@"SourceSansPro-Light" size:20.0f]; 
[buttonDone setTitle:@"Done" forState:UIControlStateNormal]; 
[customBarButtonView addSubview:buttonDone]; 

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:customBarButtonView]; 
2
_myrButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"French KeyBoard" style:UIBarButtonItemStylePlain target:self action:@selector(RbuttonItemdown:)]; 

NSDictionary* itemTextAttributes = @{NSFontAttributeName:[UIFont fontWithName:@"Georgia" size:12.0f], NSForegroundColorAttributeName:[UIColor blueColor], NSBackgroundColorAttributeName:[UIColor lightGrayColor]}; 

[_myrButtonItem setTitleTextAttributes:itemTextAttributes forState:UIControlStateNormal]; 

[self.navigationItem setRightBarButtonItem:_myrButtonItem]; 

您可以使用所有這一切:

NSString *const NSFontAttributeName ; 
NSString *const NSParagraphStyleAttributeName ; 
NSString *const NSForegroundColorAttributeName ; 
NSString *const NSBackgroundColorAttributeName ; 
NSString *const NSLigatureAttributeName ; 
NSString *const NSKernAttributeName ; 
NSString *const NSStrikethroughStyleAttributeName ; 
NSString *const NSUnderlineStyleAttributeName ; 
NSString *const NSStrokeColorAttributeName ; 
NSString *const NSStrokeWidthAttributeName ; 
NSString *const NSShadowAttributeName ; 
NSString *const NSTextEffectAttributeName ; 
NSString *const NSAttachmentAttributeName ; 
NSString *const NSLinkAttributeName ; 
NSString *const NSBaselineOffsetAttributeName ; 
NSString *const NSUnderlineColorAttributeName ; 
NSString *const NSStrikethroughColorAttributeName ; 
NSString *const NSObliquenessAttributeName ; 
NSString *const NSExpansionAttributeName ; 
NSString *const NSWritingDirectionAttributeName ; 
NSString *const NSVerticalGlyphFormAttributeName;