资源子目录中的iOS自定义字体 [英] iOS custom font in resources subdirectory
问题描述
我正在尝试将自定义字体放到Resources/Fonts/MyFontFamily子目录中,但是我在如何从代码中加载它们时遇到了问题. 在我的info.plist中,我添加了:
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>
在我的代码中
var font = UIFont.FromName(@"/Fonts/MyFontFamily/MyFont-Regular.ttf", 14);
但是字体为空,如果我将字体直接放置在Resources文件夹中,并相应地在方法UIFont.FromName中更改info.plist和字体路径,则会加载字体.是否不可能在资源"子目录中放置自定义字体?
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.
推荐答案
info.plist
<key>UIAppFonts</key>
<array>
<string>Fonts/MyFontFamily/MyFont-Regular.ttf</string>
注意:请勿在数组字符串的开头添加"/".
Note: Do not include "/" at the beginning of the array string.
注意:这假定您的字体位于Resources
的Fonts/MyFontFamily
子目录中.
Note: This assumes that your fonts are in the Fonts/MyFontFamily
subdirectory of your Resources
.
var font = UIFont.FromName(@"MyFont-Regular", 20);
注意:这是字体书
中显示的字体名称即:
Note: This is the font name as seen in the Font Book
i.e.:
PinyonScript-Regular.ttf
通过以下方式打开:
var font = UIFont.FromName(@"Pinyon Script", 20);
快速字体名称检查,在Font Book
中打开字体,然后在标题栏中查看字体名称:
Quick font name check, open the font in Font Book
and look at the name of the font in the title bar:
这篇关于资源子目录中的iOS自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!