iOS custom font in resources subdirectory

2020-04-16 03:02发布

问题:

I'm trying to place my custom fonts into Resources/Fonts/MyFontFamily subdirectory, however I'm having problems on how to load them from the code. In my info.plist I've added:

<key>UIAppFonts</key>
<array>
    <string>/Fonts/MyFontFamily/MyFont-Regular.ttf</string>

In the code I do

var font = UIFont.FromName(@"/Fonts/MyFontFamily/MyFont-Regular.ttf", 14);

However the font is null, If I place the font directly in the Resources folder and change info.plist and font path in the method UIFont.FromName accordingly it does load the font. Is it not possible to have custom fonts placed in Resources subdirectory?

Edit: I've tried both with and without "/" at the start of the path.

回答1:

info.plist

<key>UIAppFonts</key>
<array>
    <string>Fonts/MyFontFamily/MyFont-Regular.ttf</string>

Note: Do not include "/" at the beginning of the array string.

Note: This assumes that your fonts are in the Fonts/MyFontFamily subdirectory of your Resources.

Flag your fonts to Always Copy:

Code:

var font = UIFont.FromName(@"MyFont-Regular", 20);

Note: This is the font name as seen in the Font Book

i.e.:

PinyonScript-Regular.ttf

Is opened via:

var font = UIFont.FromName(@"Pinyon Script", 20);

Quick font name check, open the font in Font Book and look at the name of the font in the title bar:



回答2:

Dont think you need the "Fonts" bit at the start of the path try :

var font = UIFont.FromName(@"MyFont-Regular.ttf", 14);

OR

var font = UIFont.FromName(@"MyFontFamily/MyFont-Regular.ttf", 14);