简体   繁体   中英

Adding custom font (Impact) to iOS project not working

This is a topic that looks like many others have struggled with. I'm trying to add the 'Impact' font to my iOS project. To do so, I perform the following steps:
1. Add the Impact.ttf file to my project.
2. Ensure that the Impact.ttf file is in fact added to the project Target.
3. Check 'Copy Bundle with Resources' in the target Build Phases and make sure the Impact.ttf file is included .
4. In My-Project-Name-Info.plist, I added the "Fonts provided by application" option and set Impact.ttf as item 0 in the array.
5. Open the font file with Font Book and make sure I'm referencing the font by the correct name. In this case, Impact is the correct name.

I used the following code snipped to see all available fonts in the project, and 'Impact' is not one of them:

for (NSString *family in [UIFont familyNames]) {
    NSLog(@"%@", family); 
    for (NSString *name in [UIFont fontNamesForFamilyName:family]) {
        NSLog(@"  %@", name);
    }
}

When inspecting the value of UIFont in the following snippet, I get nil.

UIFont *impactFont = [UIFont fontWithName:@"Impact" size:36.0f]; 

I've quadruple checked all spellings and gone through each step multiple times. I've also used the following resources (and many more) to troubleshoot the problem.

If I open Impact.ttf with font book, it says that the font is not installed. 一件事如果我用字体书打开Impact.ttf,它会说没有安装字体。 I click to install the font, and get a warning that there are duplicate Impact fonts installed. To fix this warning, I click 'Resolve duplicates automatically' and the font shows as installed. However, if I open the file again, the font shows as not installed.

I'm not sure if having the font installed in Font Book is necessary for it to correctly be recognized in my project .

Thank you very much for your help! I know this question has been asked multiple times, but I feel like I've exhausted all resources and links here on SO and other sites.

Maybe this is not the answer you want, but is a solution you could use to move on. That is what i use on my projects, simple and useful https://github.com/deni2s/IBCustomFonts

Hope you can find a better solution!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM