UIFont with custom font fails with Nil

wayneh picture wayneh · Jan 28, 2012 · Viewed 9.3k times · Source

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?

Answer

wayneh picture wayneh · Jan 31, 2012

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.