1
func initializePickerViewProperties() {
    let font = UIFont (name: "SanFranciscoDisplay-Regular", size: 30.0)
    let highlightedFont = UIFont (name: "SanFranciscoDisplay-Bold", size: 35.0)
    pickerView.font = font!
    pickerView.highlightedFont = highlightedFont!
}

fairly simple, the pickerView in question is an AKPickerView

If I remove the forced unwrapping I get a compiler error. "Value of optional type UIFont not unwrapped, did you mean to use "!" or "?"?"

However, when I force unwrap it, I get a runtime error. "fatal error: unexpectedly found nil while unwrapping an Optional value"

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
chris P
  • 6,359
  • 11
  • 40
  • 84
  • 1
    Possible duplicate of [How to use new San Francisco font in iOS 9?](http://stackoverflow.com/questions/31369711/how-to-use-new-san-francisco-font-in-ios-9) – Eric Aya Feb 22 '16 at 12:41

3 Answers3

5

Try printing all available fonts, and check the spelling of your fontname

 for fontfamily in UIFont.familyNames{
        for fontname in UIFont.fontNames(forFamilyName: fontfamily){
            print(fontname)
        }
    }
Mohamed ALOUANE
  • 5,349
  • 6
  • 29
  • 60
TPlet
  • 898
  • 7
  • 13
3

Means your fonts are not initialized properly and give nil. You should safely unwrap them:

func initializePickerViewProperties() {
    if let font = UIFont (name: "SanFranciscoDisplay-Regular", size: 30.0),
        let highlightedFont = UIFont (name: "SanFranciscoDisplay-Bold", size: 35.0) {
        pickerView.font = font
        pickerView.highlightedFont = highlightedFont
    }
}
konrad.bajtyngier
  • 1,786
  • 12
  • 13
  • Thanks. Unfortunately it looks like they are never successfully unwrapped, and I never enter the IF. Any idea why this might be? Perhaps incorrect Font name? – chris P Feb 22 '16 at 12:39
  • 2
    Yes, but if you're interested by the proper way to use the San Francisco system font, and not just *any font but the system one*, follow the duplicate link I posted under the question... – Eric Aya Feb 22 '16 at 13:08
0

You must first add your custom font inside your Info.plist.

To look for the Info.plist file:

  1. Make sure you have imported your custom font onto your project.
  2. Click the blue icon on the topmost part of your project navigator--the project itself.
  3. Go to the Info tab.
  4. Look for Fonts provided by application.
  5. Then, click the plus icon to add a new key-value pair to our Info.plist.
  6. Add the exact file name of the font you imported, including the file extension.

I solved mine using this. Hope it works for you as well.

Kaye
  • 153
  • 2
  • 12