Change the font and font color of the UIBarButtonItem

I would like to know how to change the text font of BarButtonItem ? Although I could not set setTitleTextAttributes

 class TextfieldLogin : UITextField { func INIT(){ let numberToolbar = UIToolbar(frame: CGRectMake(0,0,320,50)) numberToolbar.tintColor = UIColor.whiteColor() numberToolbar.barTintColor = UIColor(red: 155.0/255, green: 14.0/255, blue: 45.0/255, alpha: 1.0) numberToolbar.items = [ UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil), UIBarButtonItem(title: "DONE", style: UIBarButtonItemStyle.Plain, target: self, action: "close"), UIBarButtonItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFontOfSize(18.0),NSForegroundColorAttributeName : UIColor.redColor(),NSBackgroundColorAttributeName:UIColor.blackColor()], forState: UIControlState.Normal) ] numberToolbar.sizeToFit() } } 
+6
source share
2 answers

Is this what you are looking for?

Objective-C:

 [buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica-Bold" size:20.0], NSFontAttributeName, [UIColor blackColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal]; 

Swift:

 let titleDict: NSDictionary = [NSFontAttributeName: "Helvetica-Bold"] self.numberToolbar.titleTextAttributes = titleDict 
+12
source

As with all UIBarItem , you can set text attributes using:

 - (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state 

You can also use tintColor to change the color of the text.

I would recommend sticking with the Apple style guide in terms of using bold fonts only in the done element.

+3
source

All Articles