Change the Font of a Uibarbuttonitem

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 :

Sample Image

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



Leave a reply



Submit