2014-02-06 7 views
9

Ich versuche, meine Schriftart UIBarButtonItem zu ändern. Es sieht gut aus, wenn ViewControllers geladen wird. Aber wenn ich auf den Balkenknopf tippe oder nach rechts wische, um zum vorherigen ViewController zu wechseln (aber dann zum aktuellen ViewController zurückzugelangen), wechselt die Schriftart zurück zur Systemschriftart. Hier ist, was ich in meinem AppDelegate bin Einstellung:iOS 7 UIBarButtonItem Schriftart ändert sich, wenn angeklickt

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

Und hier ist ein Beispiel für meine 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]; 
} 

Bin ich irgendwie die Schrift wieder zu ändern, oder bin ich zu mißbrauchen das Aussehen Proxy?

Antwort

15

Das Problem besteht darin, dass die tintColor Einstellung mit den Titeltextattributen in Konflikt steht. Kommentieren Sie diese Zeile und alles wird gut.

Wenn Sie vorhaben, Titeltext verwenden Attribute, dann die Textfarbe in diese Attribute übernehmen:

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

Dank. Das scheint wahr zu sein. Aber wie kann ich die Tastenfarbe pro ViewController ändern? – hodgesmr

+0

In iOS 7 ist NSAttributedString ein Objekt der ersten Klasse und die Farbe ist daher ein Textattribut. Anstatt den Darstellungsproxy zu verwenden und einen Konflikt mit einer einzelnen 'tintColor' zu haben, machen Sie einfach die Schaltfläche und geben Sie dem Text die gewünschten Attribute, einschließlich Schriftart und Farbe. – matt

+0

Ich habe bearbeitet, um zu veranschaulichen, was ich meine. – matt

3

Durch diesen Ansatz in viewDidLoad Methode verwenden Sie benutzerdefinierten Text festlegen können, Schriftart, Größe usw. alles auf einmal Ort.

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

können Sie alle verwenden:

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; 
Verwandte Themen