问题
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?
回答1:
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.
回答2:
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.
回答3:
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 :)
回答4:
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);
}
}
回答5:
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
回答6:
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)!
}
回答7:
Be sure to include the font's file extension as part of the name in plist "Fonts provided by application".
来源:https://stackoverflow.com/questions/9047230/uifont-with-custom-font-fails-with-nil