Random Someone
Random Someone

Reputation: 392

UIFont returning nil for custom font

I am trying to use the Nunito-ExtraBoldItalic font for my UILabel.

I followed the steps outlined in tutorials and other answers.

  1. Add font to fonts folder. Made sure the target is set to the project. enter image description here
  2. Add the entry to plist. enter image description here
  3. Made sure the font is shown in build phase/copy bundle resources. It is shown.

I am using the following code to create a UIlabel:

uiLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 160, height: 50))
let myfont = UIFont(name: "Nunito-ExtraBoldItalic", size: 14)
uiLabel!.font = myfont
uiLabel!.textColor = UIColor(red: CGFloat(51/255.0), green: CGFloat(33/255.0), blue: CGFloat(32/255.0), alpha: CGFloat(100.0))
addSubview(uiLabel!)

But UIFont returns nil.

Postscript name of font is "Nunito-ExtraBoldItalic".

I also tried running the following:

    for familyName:String in UIFont.familyNames {
        print("Family Name: \(familyName)")
        for fontName:String in UIFont.fontNames(forFamilyName: familyName) {
            print("--Font Name: \(fontName)")
        }
    }

My font is not shown.

The font is visible in storyboard and other areas.

enter image description here

I tried using the Nunito-ExtraBoldItalic, Nunito-ExtraBold Italic, Nunito-ExtraBold-Italic as names. None worked.

So I am not sure what the problem is.

Upvotes: 2

Views: 2384

Answers (2)

TheNitram
TheNitram

Reputation: 418

  1. Add the font file to the project.

enter image description here

  1. Add "Fonts provided by application" in Info.plist.

    <key>UIAppFonts</key> <array> <string>CarterOne.ttf</string> </array>

  2. Make sure the font file is listed in BuildPhase->Copy Bundle Resources.

enter image description here

  1. Make sure the Target Membership is check.

enter image description here

  1. Do a clean build cmd+option+shift+K

Upvotes: 7

arvidurs
arvidurs

Reputation: 3043

You also need to add the font to the .plist file Please read the following: https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app

Add the font name to this plist key: "Fonts provided by application"

Upvotes: 1

Related Questions