简体   繁体   English

Xcode 9自定义字体不起作用

[英]Xcode 9 custom fonts not working

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错误吗?

I have this issue ,, 我有这个问题,

make sure that you Click on your font folder to make sure they are included in your “Target Membership.” 确保单击您的字体文件夹以确保它们包含在您的“目标成员资格”中。

在此输入图像描述

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 Resources inside Build Phases tab. 如果你直接添加字体文件夹,那么你可能会面临这样的问题。它不会在Build Phases选项卡中的Copy Bundle Resources下反映你的字体文件。 To solve this problem I used following steps : 为了解决这个问题,我使用了以下步骤:

  1. First select your font.ttf file inside bundle and on right side under File Inspector check mark the Target Membership option for your project. 首先在bundle内选择你的font.ttf文件,然后在File Inspector下选中你的项目的Target Membership选项。

  2. Goto project target and under Build Phases goto Copy Bundle Resources and 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) 转到项目目标并在Build Phases下转到Copy Bundle Resources并单击+按钮添加所有字体。(确保只添加font.ttf文件而不是包含该font.ttf文件的文件夹)

  3. Also make sure your file are properly added while dragging them inside your project.Don't forget to check Copy items if needed option. 还要确保在项目中拖动文件时正确添加文件。不要忘记选中复制项目(如果需要)选项。 Most important do add the key Fonts provided by application in your info.plist file followed by all the custom fonts you used in your project. 最重要的是在info.plist文件中添加应用程序提供的关键字体,然后添加项目中使用的所有自定义字体。

Hope this helps. 希望这可以帮助。 Happy coding :) 快乐编码:)

Had the same problem after opening project in Xcode 9, solved it by reincluding the fonts. 在Xcode 9中打开项目后出现同样的问题,通过重新包含字体解决了这个问题。 (deleted them, cleaned project, included them, built project). (删除它们,清理项目,包含它们,构建项目)。

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. 然而,重新包容是最好的方式,它使一切都好。

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. 1-添加到包中的字体。 2- add in plist file font name under Fonts provided by application 3- Go to Target->build phase and add your font files. 2-在应用程序提供的字体下添加plist文件字体名称3-转到目标 - >构建阶段并添加字体文件。

Now you can run your project.It will show to the custom font in your application. 现在您可以运行您的项目。它将显示您的应用程序中的自定义字体。

它为编译器提出了一个问题,如果它位于蓝色目录文件夹中(显然)我将我的字体文件移到文件夹之外,构建并解决了问题(GRR!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM