Xcode 9 自定义字体不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46303443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 9 custom fonts not working
提问by MobileMon
Custom fonts not working in Xcode 9. However if I open the same project in Xcode 8, the custom fonts work as expected
自定义字体在 Xcode 9 中不起作用。但是,如果我在 Xcode 8 中打开同一个项目,自定义字体将按预期工作
I've ran this piece of code to ensure the fonts show up:
我已经运行了这段代码以确保字体显示:
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
{
NSString *fontFamily = [fontFamilies objectAtIndex:i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
NSLog (@"%@: %@", fontFamily, fontNames);
}
I've ensured the fonts are in the plist, checked target membership, and in the copy bundle resource section.
我已经确保字体在 plist 中,检查过目标成员资格,以及复制包资源部分。
Is this an Xcode bug?
这是一个 Xcode 错误吗?
回答by Ahmed Safadi
回答by Prashant Gaikwad
I am also using Xcode 9.2 and even I also faced same issues.
我也在使用 Xcode 9.2,甚至我也遇到了同样的问题。
What I did is I directly added the custom font folders inside my app. If you directly add fonts folder then you may face such issues.It will not reflect your font files under Copy Bundle Resourcesinside Build Phasestab. To solve this problem I used following steps :
我所做的是直接在我的应用程序中添加了自定义字体文件夹。如果你直接添加字体文件夹,那么你可能会遇到这样的问题。它不会反映你在Build Phases选项卡内的Copy Bundle Resources下的字体文件。为了解决这个问题,我使用了以下步骤:
First select your font.ttf file inside bundle and on right side under File Inspectorcheck mark the Target Membershipoption for your project.
Goto project target and under Build Phasesgoto Copy Bundle Resourcesand add all your fonts by clicking on + button.(make sure you adding your font.ttf files only not the folder containing that font.ttf file)
Also make sure your file are properly added while dragging them inside your project.Don't forget to check Copy items if neededoption. Most important do add the key Fonts provided by applicationin your info.plist file followed by all the custom fonts you used in your project.
首先在包内选择您的 font.ttf 文件,并在File Inspector下的右侧选中您项目的Target Membership选项。
转到项目目标并在Build Phases 下转到Copy Bundle Resources并通过单击 + 按钮添加所有字体。(确保仅添加 font.ttf 文件而不是包含该 font.ttf 文件的文件夹)
还要确保在将文件拖动到项目中时正确添加了文件。如果需要,请不要忘记选中“复制项目”选项。最重要的是在 info.plist 文件中添加应用程序提供的关键字体,然后是您在项目中使用的所有自定义字体。
Hope this helps. Happy coding :)
希望这可以帮助。快乐编码:)
回答by Bil Kimes
It presents a problem for the compiler if it is located inside a blue directory folder (apparently) I moved my font file outside the folder, build and problem solved (GRR!)
如果它位于蓝色目录文件夹内(显然),它会给编译器带来问题,我将字体文件移到文件夹外,构建并解决了问题(GRR!)
回答by Tomá? John
Had the same problem after opening project in Xcode 9, solved it by reincluding the fonts. (deleted them, cleaned project, included them, built project).
在 Xcode 9 中打开项目后遇到同样的问题,通过重新包含字体解决了它。(删除它们,清理项目,包含它们,构建项目)。
Altough you got them checked in the target, but not visible to chose as custom font, you can try to specify the localization (above target check) to 'base' or some, it made them visible in Storyboards after that, but unable to load in code (at least in my case). However, reincluding is imo better way to go, it made everything OK.
尽管您在目标中检查了它们,但无法选择作为自定义字体,但您可以尝试将本地化(在目标检查上方)指定为“基础”或某些,之后它使它们在故事板中可见,但无法加载在代码中(至少在我的情况下)。但是,重新包含是 imo 更好的方法,它使一切正常。
回答by Pravesh Uniyal
I solved custom font issue in my projects using Xcode 9. Here are the following steps-
我使用 Xcode 9 在我的项目中解决了自定义字体问题。以下是以下步骤 -
1- Add to the font in the bundle. 2- add in plist file font name under Fonts provided by application 3- Go to Target->build phase and add your font files.
1- 添加到包中的字体。2- 在应用程序提供的字体下添加 plist 文件字体名称 3- 转到目标-> 构建阶段并添加您的字体文件。
Now you can run your project.It will show to the custom font in your application.
现在您可以运行您的项目。它将显示您应用程序中的自定义字体。