10

I've read around for different solutions but nothing seems to work. This code creates a nil exception:

[NSFontAttributeName: UIFont(name: "Raleway-SemiBold", size: 16)!]

I have the fonts installed properly and they show up correctly in the app (target is set).
I tried to add the application provided fonts in the plist but nothing happened. I can't edit the items in the array: (they are item0 : string : Raleway-SemiBold.tff).

So basically I'm stuck... Sometimes Swift and Apple environments are great for a programmer, other times (most of the time), they are sooo faulty and need so many workarounds to reach the expected results.

Many thanks in advance for any help.

Razvan Soneriu
  • 587
  • 3
  • 7
  • 23
  • 2
    Have you verified that `UIFont(name: "Raleway-SemiBold", size: 16)` is not `nil`? – No offense meant, but I doubt that Apple or Swift is the culprit :) – Martin R Mar 25 '15 at 18:12
  • No offence taken, I don't blame Apple or Swift, I needed to do a lot of workarounds for this project and this was just the moment when I had to let it all out :). Well I guess they are nil, because the compiler says that unexpectedly found nil while unwrapping. But the fonts are included in the app and they work in interface and so on. – Razvan Soneriu Mar 25 '15 at 18:14
  • One common pitfall is to specify the wrong font name in `UIFont(name: ...)` – it is not the file name. But there must be dozens of Q&A with the exact instructions ... – Martin R Mar 25 '15 at 18:18
  • Well I just looped through the fontNames and my font isn't listed there...EDIT: actually only 4 out of 9 are listed there – Razvan Soneriu Mar 25 '15 at 18:20
  • What do you mean by *"I did try to add the application provided fonts in the plist ..."* and *"I cant name the items in the array"* ? – Martin R Mar 25 '15 at 18:22
  • I tried adding the suggested Fonts provided by application key in plist, and where I read it said it should be an array with the font name. The I cant name is a typo which I will correct – Razvan Soneriu Mar 25 '15 at 18:24

3 Answers3

19

You're getting an exception because UIFont(name: "Raleway-SemiBold", size: 16) returns nil and you're force-unwrapping it with !.

Instead, use conditional unwrapping:

if let font = UIFont(name: "Raleway-SemiBold", size: 16) {
    let attributes = [NSFontAttributeName: font]
    // do something with attributes
} else {
    // The font "Raleway-SemiBold" is not found
}

You can use UIFont's familyNames() and fontNamesForFamilyName(_:) methods to get the exact string required.

Swift 4

if let font = UIFont(name: "Raleway-SemiBold", size: 16) {
    let attributes = [NSAttributedStringKey.font: font]
    // do something with attributes
} else {
    // The font "Raleway-SemiBold" is not found
}
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • So I should loop through the fonts and see what the name is? The fonts are installed and working on other parts of the app. – Razvan Soneriu Mar 25 '15 at 18:17
  • Sure! If you don't see the font names you expect there, [see this answer](http://stackoverflow.com/a/10255264/1445366) for troubleshooting steps. – Aaron Brager Mar 25 '15 at 19:00
2

You Just have to write the correct string name of your font.

  1. Don't write the name that is font file name. (like bodoni-mt-bold.ttf its the file name i have downloaded from any site).

  2. To find out the exact font name follow the image below.

  3. Go to your label select it and go to custom font and then see the name of your custom font in its family. if your custom font name is there then copy that name and past it as a string where u wanna use it. (Note you can't copy font name text you have to write else where then past it)

enter image description here

MRizwan33
  • 2,723
  • 6
  • 31
  • 42
2

For Swift 3, here's an update that worked for me:

First you'll set up the font and then create a textAttribute with the NSFontAttributeName:

let font = UIFont(name: "Raleway-SemiBold", size: 16)
textAttribute = [NSFontAttributeName: font as Any, NSForegroundColorAttributeName: UIColor.black]

You can then apply textAttribute to your label, textfield etc.

Nii Mantse
  • 2,554
  • 2
  • 13
  • 10