1

This is strange.

This works:

titleLabel.font = UIFont(name: "OpenSans-Regular", size: 14)

This works:

attributedString.addAttribute(NSFontAttributeName, value:
    UIFont(name: "OpenSans-Bold", size: 18)!,
    range: ((attributedString.string as NSString).range(of: "Forgot your password? Reset it here")))

This doesn't. Gives a crash:

Unexpectedly found nil while unwrapping an Optional value

attributedString.addAttribute(NSFontAttributeName, value:
    UIFont(name: "OpenSans-Regular", size: 18)!,
    range: ((attributedString.string as NSString).range(of: "Email or password are incorrect."))). 

First example shows that OpenSans-Regular works fine in the app. (Same font used in 3rd example).

Second example shows attributed string works fine with custom font.

Third example shows attributed string with custom font doesn't work.

I double checked, OpenSans-Regular I correctly copied in my project :

Project directory :

enter image description here.

Copy bundle resources :

enter image description here

Info.plist :

enter image description here.

Nitish
  • 13,845
  • 28
  • 135
  • 263
  • this `UIFont(name: "OpenSans-Bold", size: 18)` is probably `nil` due to either the custom-font's name is not correct or the custom font is not supported by the app. – holex Jan 23 '18 at 17:14
  • Ensure availability of valid font by executing code with `if-let` and see what result you get – Krunal Jan 23 '18 at 17:29
  • 1
    FYI - the name you pass as the name to `UIFont(name:size:)` is often different than the base name of the ttf file. – rmaddy Jan 23 '18 at 17:30
  • What is the postscript name of that font? Also it's not NSMutableAttributedString that make it crash, it's your font, because you used a `!` saying "Crash if nil" while you didn't on setting the font in your label (which I expect didn't work). – Larme Jan 23 '18 at 17:31
  • 2
    See https://stackoverflow.com/questions/18021945/ios-ttf-fonts-only-partially-work and https://stackoverflow.com/questions/18413332/uifont-woes-some-custom-fonts-loading-but-others-are-not – rmaddy Jan 23 '18 at 17:32
  • read https://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/ and there make sure that you are using correct name of the font (see point 5 in that article) – Milan Nosáľ Jan 23 '18 at 17:33
  • 1
    @maddy : It turns out your words are 100% correct. It should be used as OpenSans instead of OpenSans-Regular as per the link suggests. Please post your answer for future reference. – Nitish Jan 23 '18 at 17:37
  • @maddy : I however suspect the question is not duplicate. They are related but definitely not duplicate. – Nitish Jan 23 '18 at 17:42
  • @Nitish No, the answer says to use `OpenSans` because it's the postscript name of the font (read the answer, use FontBook.app on your Mac), what I suggested too. And that's clearly the same solution because of the same reason of why using "OpenSans". Then it's a duplicate. – Larme Jan 23 '18 at 17:46

2 Answers2

4

You simply did not correctly add the font with name "OpenSans-Regular",

First line, line:

titleLabel.font = UIFont(name: "OpenSans-Regular", size: 14)

works, because titleLabel.font is optional, thus it has no problem with UIFont(name: "OpenSans-Regular", size: 14) being evaluated to nil. So it does not prove that "OpenSans-Regular" was added correctly.

However, in the third example:

attributedString.addAttribute(NSFontAttributeName, value:
    UIFont(name: "OpenSans-Regular", size: 18)!,
    range: ((attributedString.string as NSString).range(of: "Email or password are incorrect.")))

here you are force unwrapping it (notice ! at the end of UIFont(name: "OpenSans-Regular", size: 18)!), thus it crashes - this on the other hand proves you did not add it correctly.

EDIT

Make sure you add "OpenSans-Regular" correctly to the project. I recommend going over the list at this blog entry, especially check point number 5 there (names).

EDIT 2

As @rmaddy pointed out, "OpenSans-Regular" is "OpenSans" - see this question. Therefore rather use:

titleLabel.font = UIFont(name: "OpenSans", size: 14)

And:

attributedString.addAttribute(NSFontAttributeName, value:
    UIFont(name: "OpenSans", size: 18)!,
    range: ((attributedString.string as NSString).range(of: "Email or password are incorrect.")))
Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
3

This will avoid app crash. Force unwrapping of UIFont may be reason for app crash. Try this and see.

if let openSansRegularFont18 = UIFont(name: "OpenSans-Regular", size: 18) {
   attributedString.addAttribute(NSFontAttributeName, value:
    openSansRegularFont18,
    range: ((attributedString.string as NSString).range(of: "Email or password are incorrect.")))
} else {
  print("OpenSans-Regular is nil (not installed/added in your project)")
  // Font file name differs from the actual font name.  Use font name as "OpenSans" only.
}
Krunal
  • 77,632
  • 48
  • 245
  • 261
  • else is getting called. I wonder why is that since I added the font correctly. – Nitish Jan 23 '18 at 17:33
  • Can you share font (family) files or source of font files. I can check it with my project – Krunal Jan 23 '18 at 17:33
  • 2
    @Nitish You've added the font correctly but you are using the wrong name. See the links I just posted under the question. – rmaddy Jan 23 '18 at 17:34
  • 2
    @Nitish - Link shared by rmaddy may solve your problem. It states remove word `regular`. Just use `OpenSans` only. – Krunal Jan 23 '18 at 17:36
  • Turns out Maddy is correct. Thanks Krunal for your inputs. – Nitish Jan 23 '18 at 17:37