I have a UIBarButtonItem
in my UIToolbar
titled
These are great answers above. Just updating for iOS7:
NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Thin" size:18.0] , NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
swift 3
barButtonName.setTitleTextAttributes( [NSFontAttributeName : UIFont.systemFont(ofSize: 18.0),NSForegroundColorAttributeName : UIColor.white], for: .normal)
To be precise, this can be done as below
[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica-Bold" size:26.0], NSFontAttributeName,
[UIColor greenColor], NSForegroundColorAttributeName,
nil]
forState:UIControlStateNormal];
Or with object literal syntax:
[buttonItem setTitleTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Bold" size:26.0],
NSForegroundColorAttributeName: [UIColor greenColor]
} forState:UIControlStateNormal];
For convenience, here's the Swift implementation:
buttonItem.setTitleTextAttributes([
NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
NSAttributedStringKey.foregroundColor: UIColor.green],
for: .normal)
You can create a custom UIView
programmatically:
UIView *buttonItemView = [[UIView alloc] initWithFrame:buttonFrame];
Then add images, labels or whatever you like to your custom view:
[buttonItemView addSubview:customImage];
[buttonItemView addSubview:customLabel];
...
Now put it in your UIBarButtomItem
.
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItemView];
And finally add barButtonItem to you navigation bar.
Swift 5 Implementation
rightButtonItem.setTitleTextAttributes([
NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
NSAttributedString.Key.foregroundColor: UIColor.green],
for: .normal)
Throughout App:
if let font = UIFont(name: "AvenirNext-DemiBold", size: 15) {
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName:TOOLBAR_TITLE_COLOR], forState: UIControlState.Normal)
}