4

I want to set attributedstring to my UILabel. So I defined the attributes in this way.

let attribute1:[String:Any]=[
        NSForegroundColorAttributeName: UIColor.init(colorLiteralRed: 120.0/255, green: 173.0/255, blue: 194.0/255, alpha: 1.0),
        NSFontAttributeName:UIFont.init(name: "Bariol_Bold", size: 15.0)!]
let attributedStringGreeting=NSAttributedString.init(string: welcomeMessage, attributes: attribute1)

but I am getting this error in the line:

NSFontAttributeName:UIFont.init(name: "Bariol_Bold", size: 15.0)!]

Does that mean my custom font is not taking? But when I set the font from IB it shows.

fatal error: unexpectedly found nil while unwrapping an Optional value

How can I solve this issue? Please help me.

enter image description here

enter image description here

Randi
  • 639
  • 2
  • 6
  • 23
  • You could interpret this as something like "font not found". Are you sure it's actually named "Bariol_Bold"? You need to type the *exact* name for it to work. Also be sure you have imported font files correctly. – LinusGeffarth May 31 '17 at 06:10
  • 2
    try with `Bariol-Bold` – Jigar Tarsariya May 31 '17 at 06:11
  • Did you add it Build Phase -> Copy bundle resources – Bala May 31 '17 at 06:13
  • 1
    It's common for the actual font name to be slightly different from the file name of the font file. You need to determine the real font name of the loaded font. – rmaddy May 31 '17 at 06:14
  • Yeas I imported those files in info plist too. and I can see Bariol font when I try to set custom font in IB. I tried Bariol-Bold too but same error comes. I have updated the question with my font files picture – Randi May 31 '17 at 06:17
  • 3
    What is the postscript name of theses fonts? That's the one to use. Open them in FontBooks.app on OS X, or in your iOS app, iterate through all the fonts names to find it. – Larme May 31 '17 at 06:20
  • 1
    @Randi You need to install it, then select it, click on info, and there you'll have the postscript name. – Larme May 31 '17 at 06:30
  • I updated the picture by opening the font in FontBook. But I still don't know how to find the exact name using FontBook. How ever now it works when I used Bariol-Bold – Randi May 31 '17 at 06:30
  • @LarmeThank you so much :) – Randi May 31 '17 at 06:31
  • 2
    @Randi: There: https://stackoverflow.com/a/15985375/1801544 (once you installed it) – Larme May 31 '17 at 06:36
  • https://stackoverflow.com/questions/9047230/uifont-with-custom-font-fails-with-nil , it helped me – dengApro Nov 02 '18 at 13:24

3 Answers3

14

This commonly happens if actual font name is not same as file name, so the thing is how to find font name.

Take this steps,

  1. Add all font files in your bundle
  2. Add listing of all font files in your info.plist under key : "Font Provided By Application"
  3. Now run the following code,

    for fontFamilyName in UIFont.familyNames{
        for fontName in UIFont.fontNames(forFamilyName: fontFamilyName){
            print("Family: \(fontFamilyName)     Font: \(fontName)") 
        }
    }
    
  4. Now see the output in your console, all the available fonts are printed here, check the actual font name of your provided fonts. Now just use the same font name as below:

  5. Now, use that font name for UIFont.init(name: "Actual Font name comes here", size: 15.0)!]

Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
5

The problem is in the name. Every time it's not necessary that the file name and Font name is similar. To know the exact font name you can use the following function.

func printMyFonts() {
    print("--------- Available Font names ----------")
    for name in UIFont.familyNames {
        print(name)
        print(UIFont.fontNames(forFamilyName: name))
    }
}

Hope this helps!

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
1

Try this

let attribute1:[String:Any] = [NSFontAttributeName: UIFont(name: "Bariol-Bold", size: 18)! ,
                NSForegroundColorAttributeName : UIColor.init(colorLiteralRed: 120.0/255, green: 173.0/255, blue: 194.0/255, alpha: 1.0)]

     let attributedStringGreeting=NSAttributedString.init(string: welcomeMessage, attributes: attribute1)
Mahesh Narla
  • 342
  • 2
  • 3
  • 20