UIFont with custom font fails with Nil
Asked Answered
H

7

12

I'm trying to add a custom font to my project in Xcode 4.2, but whenever I try to use it, I get a error that the object is nil.

I have done the following:
1) Added a row to my .plist 'Fonts provided by application' value: "LCDMono2 Ultra.ttf"
2) Added the font to my Supporting Files and showed it in XCode to verify it was added.
3) Verified using Get Info that the Full Name is "LCDMono2 Ultra"
4) Created the font in my project with:

UIFont *myFont = [UIFont fontWithName:@"LCDMono2 Ultra" size:16];

and I've tried this variant:

UIFont *myFont = [UIFont fontWithName:@"LCDMono2 Ultra" size:16.f];

5) Tried to use the font name (addObject:myFont.fontName), generating the 'nil' error.

What could be causing the error? Could it be something like the space in the name?

Hattiehatton answered 28/1, 2012 at 17:21 Comment(3)
Use [UIFont familyNames] to see if your font is actually getting "seen" by the OS. That will at least narrow things down. I've had som kinds of font files iOS wouldn't recognize.Basuto
@Basuto - I just did that before I read your reply! And sure enough, Xcode removes the space automatically, and the name shows up as "LCDMono2Ultra" and works. Isn't that always the way? Thanks.Hattiehatton
Probably not Xcode. That's probably the way the font name is encoded in the font file. One little gratuitous hint: iOS only lets you load two faces from a family so if you're using more than two faces from a family, you'll need to change the family name on each face to be unique. That took a while to track down ...Basuto
H
15

The space in the Full Name of the font was removed automatically after adding it to the project. I've checked the original font file, and the space is there, so Apple must not want to deal with spaces. I changed the reference to "LCDMono2Ultra" and it works.

Hattiehatton answered 31/1, 2012 at 22:48 Comment(4)
It's not a "don't want to deal with spaces," I believe it's using the PostScript name of the font.Equivocal
David Dunham is correct. In general, if you're going to actually hard-code the name of a font in your code, you should always use the full PostScript name to refer to the font. (It is the PostScript name itself that doesn't allow for spaces). If you're not sure what it is, install the font on your Mac with Font Book, then in Font Book, choose Preview > Show Font Info to show the various names, including the PostScript name. The PostScript name of "LCDMono2 Ultra" is LCDMono2Ultra.Endocardial
Yep, you're right. I didn't install the font on my system, so I couldn't get that info from font book. Once I installed it, I can see the Postscript name. Thanks.Hattiehatton
Thank you - could not understand what was happening here. Had a system font but iOS was looking for the string without spaces.Abdella
V
13

I had this exact same problem for a few hours and tried all of the above and none worked, the font was being added everywhere it should have been and I even tried fonts which worked in other apps, but never appeared to be added to this app.

It appears something has changed in Xcode 5 the font had to be added in the target properties under the info tab for this to work whereas previously they had to be added in the "appName-info.plist' see image below:

Hope this helps someone else.

enter image description here

Velure answered 2/12, 2013 at 17:3 Comment(1)
Important: add the whole filename, with extension.Mansuetude
W
3

In my case, I needed to remove all the extra hyphens from the font name, aside from the one right before the font weight:

    UIFont(name: "SF-Pro-Display-Semibold", size: 15) // fails
    UIFont(name: "SFProDisplay-Semibold", size: 15) // succeeds
Weighty answered 15/5, 2019 at 21:22 Comment(0)
I
2

Have you put a breakpoint in and checked that you are getting a valid UIFont object for myFont. This is worth checking as most likely the issue is that "LCDMono2 Ultra" is actually the font family name and what you want to be using is something like "LCDMono2 Ultra-normal" or "LCDMono2 Ultra-bold". Open your .ttf with Font Book and see what info it gives you.

Alternatively if you can't find anything in Font Book you could try calling:

NSArray *fonts = [UIFont fontNamesForFamilyName:"LCDMono2 Ultra"];

And then just printing the array fonts in the debugger to check what they should be.

Hope this help, let me know if this doesn't work and i'll try to think of something else :)

Inflexible answered 28/1, 2012 at 17:27 Comment(1)
Thanks, but I found the solution - see my comment above.Hattiehatton
B
2

Find your font (maybe with another name) and use this name.

    for (NSString *familyName in [UIFont familyNames]) {
        for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
            NSLog(@"%@", fontName);
        }
    }
Babara answered 4/4, 2017 at 9:14 Comment(0)
F
1

My font file is "Futura Heavy.ttf", then I use - replaced the space like below then it works!

    class func futuraHeavy() -> UIFont {
        return UIFont(name: "Futura-Heavy", size: 12)!
    }
Franke answered 9/8, 2017 at 14:54 Comment(0)
K
0

Be sure to include the font's file extension as part of the name in plist "Fonts provided by application".

Karafuto answered 27/2, 2019 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.