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) Ad
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.
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
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.
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)!
}
Be sure to include the font's file extension as part of the name in plist "Fonts provided by application".
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 :)