Change the font of a UIBarButtonItem
Because UIBarButtonItem inherits from UIBarItem, you can try
- (void)setTitleTextAttributes:(NSDictionary *)attributes
forState:(UIControlState)state
but this is for iOS5 only. For iOS 3/4, you will have to use a custom view.
Setting font size of UIBarButtonItem
You have to set title edge insets
With CustomView
var btn = UIButton.buttonWithType(UIButtonType.System) as! UIButton
btn.frame = CGRectMake(10, 20, 50, 50)
btn.setTitle("➞", forState: UIControlState.Normal)
btn.titleLabel?.font = UIFont(name: "Helvetica" , size: 17)
btn.titleEdgeInsets = UIEdgeInsetsMake(5, 0, 0, 0)
btn.addTarget(self, action: Selector("navigateToViewTwo"), forControlEvents: UIControlEvents.TouchUpInside)
var right = UIBarButtonItem(customView: btn)
With Normal as you have done
var rightItem = UIBarButtonItem(title: "➞", style: UIBarButtonItemStyle.Plain, target: self, action: "navigateToViewTwo")
rightItem.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Helvetica", size: 17.0)!], forState: UIControlState.Normal)
rightItem.setTitlePositionAdjustment(UIOffsetMake(0.0, 5.0), forBarMetrics: UIBarMetrics.Default)
Set both for comparison, just add it which looks batter :
navigationController?.navigationBar.topItem?.rightBarButtonItems = [rightItem,right]
Results :
Hope it will help you.
iOS 13 UIBarButtonItem Font
You'll want to look into the new UINavigationBarAppearance
classes in iOS 13.
In your case:
let style = UINavigationBarAppearance()
style.buttonAppearance.normal.titleTextAttributes = [.font: #YOURFONT#]
style.doneButtonAppearance.normal.titleTextAttributes = [.font: #YOURFONT#]
navigationController?.navigationBar.standardAppearance = style
// You may want these as well:
navigationController?.navigationBar.scrollEdgeAppearance = ...
navigationController?.navigationBar.compactAppearance = ...
Edit:
Documentation - https://developer.apple.com/documentation/uikit/uinavigationbarappearance
Changing UIBarButtonItem font on iOS 13
The way iOS handles global appearances appears to have changed. You'll need to add a condition check for iOS 13 and above and then add your attributes as shown below.
if #available(iOS 13.0, *) {
let standard = UINavigationBarAppearance()
standard.configureWithTransparentBackground()
// TITLE STYLING
standard.titleTextAttributes = [.foregroundColor: UIColor.white, .font: "FONTNAME"]
// NAV BUTTON STYLING
let button = UIBarButtonItemAppearance(style: .plain)
button.normal.titleTextAttributes = [.foregroundColor: .white, .font: "FONTNAME"]
standard.buttonAppearance = button
let done = UIBarButtonItemAppearance(style: .done)
done.normal.titleTextAttributes = [.foregroundColor: .white, .font: "FONTNAME"]
standard.doneButtonAppearance = done
UINavigationBar.appearance().standardAppearance = standard
} else {
// Your previous styling here for 12 and below
}
Check out this post for more. I found it really useful in understanding the new updates.
How can I change the default font for UIBarButtonItems?
iOS 13 before
class AppDelegate : NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if #available(iOS 13, *) {
}else{
let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 30)]
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, for: .normal)
UINavigationBar.appearance().titleTextAttributes = attributes
}
return true
}
}
iOS 13
class MyNavigationController : UINavigationController {
override init(rootViewController: UIViewController) {
super.init(rootViewController: rootViewController)
if #available(iOS 13, *) {
let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 30)]
let appearance = UINavigationBarAppearance()
appearance.buttonAppearance.normal.titleTextAttributes = attributes
appearance.titleTextAttributes = attributes
self.navigationBar.standardAppearance = appearance
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
How can I make this UIBarButtonItem Bold?
A simple solution is to change the style of the UIBarButtonItem
to .done
Changing font color of UIBarButtonItem
What about using:
func setTitleColor(UIColor?, for: UIControlState)
Documentation says it sets the color of the title to use for the specified state.
sortButton.setTitleColor( .red, for: .normal)
Related Topics
Get Uiscrollview to Scroll to the Top
iOS App, Programmatically Get Build Version
Apply Gradient Color to Arc Created with Uibezierpath
How to Add Headerview in Uicollectionview Like Uitableview's Tableheaderview
How to Load Custom Cell ( Xib) in Uicollectionview Cell Using Swift
How to Get the Hour of the Day with Swift
How to Obtain Country, State, City from Reversegeocodecoordinate
Mixing Static and Dynamic Sections in a Grouped Table View
How to Make Cmake Use Specific Compiler and Flags When Final Compilation Stage Instead of Detection
Xcode/Simulator: How to Run Older iOS Version
How to Dismiss Keyboard When Touching Anywhere Outside Uitextfield (In Swift)
Uiscrollview in Storyboard Not Working with iOS 8 Size Classes and Autolayout
Any Way to Print in Color with Nslog
iOS 9 - "Attempt to Delete and Reload the Same Index Path"
New React Native Project with Old Version of React Native
Prevent Duplicate Symbols When Building Static Library with Cocoapods