Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

ios - How can I change the font of the back button for my navigation bar?

How can I change the font of the back button for my navigation bar.

The back button is either "back" or the title from the previous view controller.

I thought this viewDidLoad would work:

navigationController?.navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "FONTNAME", size: 20)!], forState: UIControlState.Normal)

but the leftBarButton? optional returns nil.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you need to change font style entirely across you app (aka. each navigation button), preferred method is to use UIBarButtonItem.appearance() proxy.

Sample code snippet would look like this:

SWIFT 3.0+

//make sure font name u use below *actually* exists in the system !
//if it doesn't app will crash because we're force unwrapping an optional (see below) !!!
let customFont = UIFont(name: "customFontName", size: 17.0)!
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], for: .normal)

It is wise to put this code snippet somewhere at the beginning of your AppDelegate.swift file because font stylization has to happen each time the app launches. Also, you're safe to put this code in any other place (eg. Presenter class) where you do your UI stylization and customization. As soon as this code gets executed, all your BarButtons will be customized thereafter.


BUT as a true Swift ??, you should first optionally unwrap your font and eventually fallback to system font if it's not found or any other possible issue arise during font loading.

var textAttributes: [String:Any]

//custom font setup
let fontColor = UIColor.purple
let barItemCustomFont = UIFont(name: "??", size: 14)  //note we're not forcing anything here

//can we use our custom font ??
if let customFont = barItemCustomFont {
    //hooray we can use our font ??
    textAttributes = [NSForegroundColorAttributeName: fontColor, NSFontAttributeName: customFont]
} else {
    //?? not found -> omit setting font name and proceed with system font
    textAttributes = [NSForegroundColorAttributeName: fontColor]
}

//finally
UIBarButtonItem.appearance().setTitleTextAttributes(textAttributes, for: .normal)

Real world example

In a real app you would usually need to customize both UINavigationBar and UIBarButton font style (aside from other parameters) to make them visually consistent. Here is handy stylizeNavigationFontOfMyAmazingApp?? function you can use:

    func stylizeNavigationFontOfMyAmazingApp??() {
        //custom font
        let customFont = UIFont(name: "someFancyFont", size: 16)!  //note we're force unwrapping here

        //navigation bar coloring:
        UINavigationBar.appearance().tintColor = UIColor.white
        UINavigationBar.appearance().barTintColor = UIColor.blue

        //unique text style:
        var fontAttributes: [String: Any]
        fontAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: customFont]

        //navigation bar & navigation buttons font style:
        UINavigationBar.appearance().titleTextAttributes = fontAttributes
        UIBarButtonItem.appearance().setTitleTextAttributes(fontAttributes, for: .normal)

   }

Advantages of using appearance() proxy

  • Two liner code snippet for stylizing every UIBarButtonItem in your app/project.
  • You can mix appearance() proxies of several classes to get truly unique visual style
  • If you need further customization control you can re-customize each button further (e.g. placing custom views, button, images, etc) - on specific instance of UIBarButtonItem.

Reference

Apple Docs on appearance protocol.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...