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
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.