Daliya chacko
Daliya chacko

Reputation: 159

Custom font in swift 4 make Fatal error: Unexpectedly found nil while unwrapping an Optional value

I have to add custom font to my app. I added fonts to my project and listed infolist.

 enum Proxima: String {
        case regular = "Regular"
        func font(size: CGFloat) -> UIFont {
            return UIFont(name: "Proxima-\(rawValue)", size: size)!
        }
    }

Fatal error: Unexpectedly found nil while unwrapping an Optional value while calling

self.headTitle.font = UIFont.Proxima.regular.font(size: 30)

UPDATE

I added the target and also checked the copy bundle resources all are correct but still i got same error.

enter image description here enter image description here

info.plist

Upvotes: 7

Views: 4498

Answers (8)

Lov Niveriya
Lov Niveriya

Reputation: 11

Go to Identity and type and check does that font is added for project or not I am attaching ScreenShot for referral select that from right side as I checked Playo on that

Upvotes: 1

christostsang
christostsang

Reputation: 1841

If you follow this Apple guide and still have problems it is most certain that the problem is with the font name. To check the correct font name, install the font on your Mac, and open it. Then click the desired font and then the "i" icon for more information:

enter image description here

Make sure that this is the name you use in both:

  • Info.plist
  • In your enum (the raw value)

enter image description here

enter image description here

PS: If you try to use licensed fonts, then it will not work with the method above. I tried to use Bariol (duh!), but it would not recognize them. I assume there is a way to integrate your license, but I did not spend the time to do it, I just switched to a Google Font instead.

Upvotes: 6

heyfrank
heyfrank

Reputation: 5647

Xcode 11.1, iOS 13.1

I did all things right, but one custom font was missing in the list of fonts (via UIFont.fontNames(forFamilyName: fontFamilyName)).

Then I reordered the missing font in Info.plist (Fonts provided by application) to the front and it worked, yay.

Maybe this helps someone.

Upvotes: 0

Thermech
Thermech

Reputation: 4451

Be sure that your files are added to your target

Also, don't forget to add them into the "Font provided by application" section of the Info.plist

Upvotes: 8

Niilesh R Patel
Niilesh R Patel

Reputation: 707

Try these

  • Add custom files in your project and also verify in Copy Bundle Resource in Build Phase

  • Using this function you can check fonts available in your project. add this function in appDelegate and call it from didFinsihLaunching.

    func printFonts() {
      let fontFamilyNames = UIFont.familyNames
      for familyName in fontFamilyNames {
          print("------------------------------")
          print("Font Family Name = [\(familyName)]")
          let names = UIFont.fontNames(forFamilyName: familyName)
          print("Font Names = [\(names)]")
        }
      }
    
  • If your custom font is available in printed fonts list then you can use.

Upvotes: 0

AtulParmar
AtulParmar

Reputation: 4570

Run the following code to check available font in your project.

for fontFamilyName in UIFont.familyNames{
    for fontName in UIFont.fontNames(forFamilyName: fontFamilyName){
        print("Family: \(fontFamilyName)     Font: \(fontName)") 
    }
}

See the output in your console, all the available fonts are printed here, check the actual font name of your provided fonts.

And use that font name for UIFont(name: "FontName", size: 15)

Upvotes: 0

iamVishal16
iamVishal16

Reputation: 1780

Remove Regular for font because iOS uses a default font as regular try modifying code your code and It's working fine after that.

Ex:

@IBOutlet weak var label1: UILabel!
enum OpenSans: String {
    case regular = ""
    case semiBold = "-Semibold"
    func font(size: CGFloat) -> UIFont {
        return UIFont(name: "OpenSans\(rawValue)", size: size)!
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib..

    label1.font = OpenSans.regular.font(size: 30.0)

    label1.font = OpenSans.semiBold.font(size: 30.0)
}

If still you have a problem Please refer to this demo project

FontDemo

Hope it will help.

Upvotes: 1

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16426

It seems that you have not added fonts into your target

Select your fonts and from right panel check the target

enter image description here

Upvotes: 3

Related Questions