Objective-c – UIFont with custom font fails with Nil

custom-fontiphoneobjective cuifont

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?

Best Answer

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

Related Topic