If you need to completely change the font style entirely in the entire application (that is, for each navigation button), the preferred method is to use the UIBarButtonItem.appearance() proxy.
A sample code might look like this:
SWIFT 3.0+
//make sure font name u use below *actually* exists in the system ! //if it does not 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 advisable to place this piece of code somewhere at the beginning of your AppDelegate.swift file, because font styling should happen every time the application starts. In addition, you can put this code in any other place (for example, the Presenter class) where you style and customize your user interface. Once this code is executed, all of your BarButton buttons will be configured after that.
BUT like a real Swift 🤓, you must first optionally expand your font and eventually use the system font if it is not found or if there is any other possible problem 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 application, it is usually required to adjust the font style of the UINavigationBar and UIBarButton (among other parameters) to make them visually consistent. Here's a handy stylizeNavigationFontOfMyAmazingApp🎨 function that 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)
}
Benefits of using a proxy server appearance()
- Two-line code snippet to style each
UIBarButtonItem in your application / project. - You can mix the
appearance() proxy of several classes to get a truly unique visual style. - If you need additional control over the settings, you can reconfigure each button further (for example, place custom views, buttons, images, etc.) for a specific instance of
UIBarButtonItem .
Link
Apple Docs by appearance protocol.
Vexy Feb 05 '15 at 15:10 2015-02-05 15:10
source share