Search code examples
swiftfontsuinavigationcontrolleruinavigationbaruinavigationitem

How do I add a downloaded custom font to a navigationBar title in Swift?


I downloaded a cool font, and want to use it as the nav bar title for my app, but can't seem to get the app to recognize/display it. An exception is thrown every run attempt. Here are the steps I took.

  1. I moved the myCustomFont.ttf file into the project as I do other supporting files (images, etc.)
  2. Updated my info.plist file by adding Font provided by application, and adding the name of the file to the array
  3. Then, I added the following code to AppDelegate.swift


// Navigation bar customization
let navigationController = window!.rootViewController as UINavigationController

navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName : (UIFont(name: "myCustomFont", size: 10))!]

The above code works if I replace myfont with something standard like Times New Roman or Arial.

Any help would be much appreciated. Thanks!


Solution

  • Be sure your font is in Bundle Resources. For some reason Xcode it is not importing custom font properly most of the time:

    enter image description here

    and try this in the AppDelegate

    UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : UIFont(name: "JennaSue", size: 20)!, NSForegroundColorAttributeName : uicolorFromHex(0x5C8CA7)]