Custom Font in ios not working

前端 未结 17 1862
北海茫月
北海茫月 2020-12-07 22:53

I want to use HelveticaNeue-UltraLight in my ios application. I\'m adding the font file as a resource to my project and adding the \"Fonts provided by application\" key in t

相关标签:
17条回答
  • 2020-12-07 23:00

    Not only does the file needs to be added to the project, but it needs to be added to the target as well.

    Sometimes (as in my case), when drag and dropping the ".ttf" file in Xcode, the "add to target" is not checked. This means the file is actually visible in your project, but it is not embedded in the app.

    To make sure it is:

    • Click on the project's name (left pane)
    • Then on the target (middle pane)
    • Then "Build Phases" (third tab on the right pane)
    • Then "Copy Bundle Resources"

    You should see the font file in that list.

    0 讨论(0)
  • 2020-12-07 23:03

    Open Font Book application. If you installed the fonts yourself, go to user, look for the fonts you want and use the PostScript name of the font in your xcode project.

    It should work even for different font variations of the same family.

    0 讨论(0)
  • 2020-12-07 23:04

    After spending hours, I came to know that the Name for the font we need to add is not the file name of the font but the actual Font name. I was trying to include font file bonveno.ttf. I have included the filename along with extension in info.plist and was able to see the font in the Copy Bundle Resources list. My code was like

    self.label.font = [UIFont fontWithName:@"bonveno" size:30];
    

    that was causing the problem. Then I double clicked the font file from Finder to see the preview of the font. That time I noticed the name of the font in the preview window as BonvenoCF. So I added that name in the code like

    self.label.font = [UIFont fontWithName:@"BonvenoCF" size:30];
    

    Worked like charm!! Can't live without this cute font in my apps.

    0 讨论(0)
  • 2020-12-07 23:08

    In order to use custom fonts in iOS, just add your fonts by drag & drop to your project. Mention the array of font names with extension in Info.plist with key "Fonts provided by application". Then print all font family names using below code :

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

    Console print :

    Arial Hebrew ArialHebrew-Bold ArialHebrew-Light ArialHebrew

    Calibri Calibri-Bold Calibri

    Georgia Georgia-BoldItalic Georgia-Bold Georgia-Italic Georgia

    Then from console, you will get the actual font-names (As above). Use below code to create font -

    In core text :

    CTFontRef fontRef = CTFontCreateWithName((CFStringRef)fontName, fontSize, NULL);
    

    In UIKit :

    UIFont *font = [UIFont fontWithName:fontName size:10];
    

    Note : here fontName = @"Calibri-Bold" is without extension.

    0 讨论(0)
  • 2020-12-07 23:09

    Another thing to check is on a Mac: Open the app Font Book. Then go file -> file validation and open the font. If something is failing (even with a warning) in there iOS generally rejects loading the font in the app with no errors.

    If this is the case then you can fix the errors by opening the font in a free app called "Typelight" And resaving the font "Save as".

    0 讨论(0)
  • 2020-12-07 23:09
    1. Drag your font files (.ttf or .otf) into project's bundle.
    2. Then select all those fonts > on your xcode's right panel check the Target membership is enabled, if not enable it.
    3. Select project from the navigator > select Build phases > under Copy Bundle Resources verify all your fonts are listed. If not, add one by one using + icon.
    4. Info.plist > select Fonts provided by application > add your fonts one by one as Helvetica-Bold.ttf.
    0 讨论(0)
提交回复
热议问题