Applying custom fonts to controls within a Storyboard

suggest change

The following example shows how to apply custom fonts to a navigation bar and includes fixes for some quirky Xcode behavior. The same font can be applied to other controls, such as UILabel and UIButton, from the Attributes inspector after the font is added to the project. Working samples and videos are linked near the bottom.

  1. Select Your Navigation Bar within your Navigation Controller

  1. Change the Title Font in the Attributes Inspector

(You will likely need to toggle the Bar Tint for the Navigation Bar before Xcode picks up the new font)

Notes (Caveats)

Verified that this does work on Xcode 7.1.1+. (See the Samples below)

  1. You do need to toggle the nav bar tint before the font takes effect (seems like a bug in Xcode; you can switch it back to default and font will stick)
  2. If you choose a system font ~ Be sure to make sure the size is not 0.0 (Otherwise the new font will be ignored)

  1. Seems like this works with no problem when only one NavBar is in the view hierarchy. It appears that secondary NavBars in the same stack are ignored. (Note that if you show the master navigation controller’s navBar all the other custom navBar settings are ignored).

Gotchas (deux)

Some of these are repeated which means they are very likely worth noting.

  1. Sometimes the storyboard xml gets corrupt. This requires you to
review the structure in Storyboard as Source Code mode (right click
the storyboard file > Open As ...)
  1. In some cases the navigationItem tag associated with user defined runtime attribute was set as an xml
child of the view tag instead of the view controller tag. If so
remove it from between the <view></view> tags for proper operation.
  1. Toggle the NavBar Tint to ensure the custom font is used.
  2. Verify the size parameter of the font unless using a dynamic font style
  3. View hierarchy will override the settings. It appears that one font per stack is possible.

Result

Samples

Handling Custom Fonts

Note ~ A nice checklist can be found from the Code With Chris website and you can see the sample download project.

If you have your own font and want to use that in your storyboard, then there is a decent set of answers on the following SO Question. One answer identifies these steps.

  1. Get your custom font file (.ttf or .ttc).
  2. Import the font files into the Xcode project.
  3. In Info.plist, add the key “Fonts provided by application” (UIAppFonts). It is an array. Add each font file name, including the extension.
  4. In the storyboard, select the navigation bar and open the Attributes inspector. Click the font button, choose Custom, and select the family of the embedded font.

Custom Font Workaround

So Xcode naturally looks like it can handle custom fonts on UINavigationItem but that feature is just not updating properly (The font selected is ignored).

To workaround this:

One workaround is to add a UIView (UIButton, UILabel, or another UIView subclass) to the view controller, not to the navigation item. Xcode does not apply a custom font set directly on the navigation item. Set the font on that control in the storyboard, connect it as an outlet, and assign it to UINavigationItem.titleView. You can also set the title text in code. Reported as bug 23600285.

@IBOutlet var customFontTitleView: UIButton!

//Sometime later...    
self.navigationItem.titleView = customFontTitleView

Note - This example is derived from an answer I posted on SO (here).

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents