问题
I've had hit-or-miss success with using custom fonts in an iOS app.
I follow these steps:
- Drag in the font into my project and copy into the project
- Make sure the font is included in the Build Phases
- Add the font name (including the extension) into the app's plist file. I confirmed that I am using the postscript name: SourceSansPro-ExtraLight.ttf
Try to use the font
self.daysLeftLabel.font = [UIFont fontWithName:@"SourceSansPro-ExtraLight" size:14];
However, only the generic system font is used.
If I run this code sample in my code:
for (NSString* family in [UIFont familyNames])
{
NSLog(@"%@", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(@" %@", name);
}
}
The Source Sans Pro font family isn't included in this, so something isn't working.
The thing is, I followed the exact same sequence for other custom TTF fonts and they work as expected. As far as I can tell, I am doing everything right -- including the often-missed postscript name in the plist file along with the extension.
So, I'm beginning to wonder if some TTF fonts are compatible with iOS and some others aren't. Is this the case?
回答1:
I have the same issue too, you have to add actual filename in info.plist file instead of font name. while defining font in UILabel you have to use true font name.
回答2:
Severals reasons for this problem, you have to check the following:
- Make sure you ticked the checkbox for Copy Item into destinaton's group folder
- Make sure the font included in the target. You can check the Target Membership
- Check your file exists in bundle resources. Go To Target->Build Phases->Copy Bundle Resources
Add your fonts in Application plist
Find font name. It may not be same as the file name. So you can use your for loop to find the font name.
for (NSString* family in [UIFont familyNames]) { NSLog(@"%@", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family]) { NSLog(@" %@", name); }
}
Use this font name as
label.font = [UIFont fontWithName:@"font-name" size:20];
来源:https://stackoverflow.com/questions/20416184/using-fonts-ttf-or-odf-in-ios-applications-is-it-a-hit-or-miss-whether-they