简体   繁体   中英

How do I change the font of a UIBarButtonItem in Swift?

I'm generally a patient debugger, spending lots of time Googling and searching for an answer to my problem. However, my patience has run out and I'm beginning to believe this is simply not possible.

However, I am working on an iOS application using a navigation bar, and have chosen to use the "Optima" font for my entire user interface. I've been able to change the font of the title of my navigation bar, and also a custom navigation bar button on my initial view. But, on any subsequent views the font on my back buttons is default to the system. For the life of me, I can't find a way to change this.

I've tried about everything that came up on a search, and the closest success I've had is from this blog post .

This code below worked for me, but to change my back button to an image of course, not changing the font as I'd hoped .

self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "Tab_Trophy")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "Tab_Trophy")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

(By the way, the code above is in the viewDidLoad() of the view my back button would SEND the user to)

Anyway, I've tried using the setTitleTextAttributes below in various places as well, but to no avail.

setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Optima", size: 19.0)!], forState: .Normal)

So my question is: can it be done? Or should I make an image with the text in the font I want and utilize the code above that does work?

The following code will change the font of both the top 'navbar' and bottom 'toolbar' items (UINavigationBar.appearance().titleTextAttributes only changes the navbar).

Notes:

  • It sets the navbar font using:

    navigationController.navigationBar.titleTextAttributes

  • It sets the toolbar items by looping through each of them and using:

    toolbarItem.setTitleTextAttributes

  • It runs in viewDidLayoutSubviews so that items added to the toolbar in Storyboard will also be affected. If you do everything programatically you may be able to place this in viewDidLoad or viewWillAppear.
  • The boolean firstShow is just to prevent this code running every time the user rotates their device. Not strictly necessary (and unnecessary if you move the code to viewDidLoad/viewWillAppear as described above) but a little more efficient.
  • The unwrapping of the font may appear a little pedantic but otherwise the app will crash if Apple withdraw your named font in a future version of iOS.
  • It does not specify a colour for the button bar items (doing so messes up the highlighted/not-highlighted colouring of them) - instead use the default app tint (eg in Storyboard) to specify their colour.
  • It does specify a colour for the navbar (the app tint is ignored for the navbar title but it is immune to the highlighted/not-highlighted problem in its buttons).
  • Change "AmericanTypewriter", the colour and add other attributes as required.
  • Share and Enjoy.

     var firstShow = false override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() if firstShow { let navbarAttributes:[String:Any] let toolbarAttributes:[String:Any] if let font = UIFont(name: "AmericanTypewriter", size: 22.0) { navbarAttributes = [ NSFontAttributeName: font, NSForegroundColorAttributeName : UIColor.init(red:1.0, green:0, blue:0, alpha:1.0) ] toolbarAttributes = [ NSFontAttributeName: font ] } else { navbarAttributes = [ NSForegroundColorAttributeName : UIColor.init(red:1.0, green:0, blue:0, alpha:1.0) ] toolbarAttributes = [:] } navigationController?.navigationBar.titleTextAttributes = navbarAttributes if let toolbarItems = navigationController?.toolbar.items { for toolbarItem in toolbarItems { toolbarItem.setTitleTextAttributes(toolbarAttributes, for: .normal) toolbarItem.setTitleTextAttributes(toolbarAttributes, for: .disabled) } } firstShow = false } } 

You can change the font of all UIBarButtonItem labels for the app you can do this:

UINavigationBar.appearance().titleTextAttributes = [
    NSFontAttributeName: UIFont(name: "Optima", size: 19.0)!
]

Alternately, if you only want to customise a single label, you can use a custom button with something like:

let button = UIButton()
// Customise button as required
let barButtonItem = UIBarButtonItem(customView: button)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM