Custom Font in ios not working

前端 未结 17 1945
北海茫月
北海茫月 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: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.

提交回复
热议问题