3

I have a problem with a custom font in my project. I tried both ways: by code and interface builder.

I added TeamSpiritNF.ttf to the project and added it to the info.plist (Fonts provided by application).

Then in the code I entered this:

self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "TeamSpiritNF", size: 30)!]

When entering the screen in the simulator the App crashes:

fatal error: unexpectedly found nil while unwrapping an Optional value

But the same line works fine if I change the font name to Helvetica for example.

And if I use this font for a label in the interface builder (where I can select it from the dropdown), it is shown correct in the preview. But when I run the application in the simulator entering that screen is delayed and the label uses Helvetica Neue instead of TeamSpiritNF.

Anyone has an idea what the problem could be?

user3333917
  • 33
  • 1
  • 3
  • possible duplicate of [Can't use custom font in iOS app](http://stackoverflow.com/questions/9053597/cant-use-custom-font-in-ios-app) – David Berry Jan 22 '15 at 17:35
  • The referenced question has several answers that lay out exactly what you need to do to use custom fonts in an iOS application. – David Berry Jan 22 '15 at 17:36

2 Answers2

15
  1. Create a new folder in your project directory and move your custom fonts there.
  2. In the left sidebar, select your target, then Build Phases and you should see Copy Bundle Resources.
  3. Make sure your fonts are included in the list. If they aren't, include them by clicking on the + button.
Cesare
  • 9,139
  • 16
  • 78
  • 130
  • Solution didn't worked for me still getting the same error fatal "error: unexpectedly found nil while unwrapping an Optional value" – Manu Gupta May 15 '15 at 06:01
  • Are you specifing the name of the font via code, right? If so, probably you probably got the name of the font wrong. – Cesare May 15 '15 at 10:53
  • i have copy pasted the font name. They are working fine when i use the same font via storyboard – Manu Gupta May 15 '15 at 12:11
7
  1. Make sure your fonts are in info.plist (Fonts provided by application)
  2. Select yor target, in the build phases tab, Copy Bundle Resources, add your fonts.
Mario Jaramillo
  • 567
  • 6
  • 9
  • my fonts were working fine, but after i tried to clean osx fonts cache, it stopped working. adding this made things go back to normal for whatever reason. – joe Jun 18 '16 at 07:12