简体   繁体   English

如何在Cocos2D iOS项目中添加字体并将其与CCLabelTTF一起使用?

[英]How can I add a font to my Cocos2D iOS project and use it with a CCLabelTTF?

I need to use a custom font with my Cocos2D project. 我需要在我的Cocos2D项目中使用自定义字体。 I've tried a few techniques, but Cocos2D keeps reporting: 我尝试了一些技巧,但Cocos2D不断报道:

Unable to load font XXX.ttf 无法加载字体XXX.ttf

Why can't I get my custom font to work with with Cocos2D? 为什么我不能使用Cocos2D使用自定义字体?


ADDITIONAL INFORMATION 附加信息

This is a Question+Answer post. 这是一个问题+答案的帖子。 This has been a problem that has had me beating my head against a wall all morning. 这是一个让我整个早上都撞在墙上的问题。 After reading through numerous solutions, across numerous websites, I've discovered that an amalgam of all available solutions was necessary to fix this issue. 在阅读了众多网站的众多解决方案之后,我发现所有可用解决方案的合并都是解决这个问题的必要条件。

What has further confounded the problem for some individuals is that you can sometimes get your application to work, perfectly, in the simulator but it won't work on your iOS device! 对某些人来说进一步困扰的是,你有时可以让你的应用程序在模拟器中完美地工作,但它不适用于你的iOS设备! Furthermore, I have also read of a couple instances where users have had the opposite problem where the device will work properly, but not the simulator. 此外,我还读过几个用户遇到相反问题的情况,其中设备可以正常工作,但不是模拟器。 I haven't experienced the latter problem but I believe following these instructions will help solve this issue, for everyone. 我没有遇到后一个问题,但我相信遵循这些说明将有助于解决这个问题。

If you've had this problem, solved it yourself and I missed a step, please post a comment. 如果您遇到此问题,请自行解决,我错过了一步,请发表评论。 I'll update my post to include any additional details. 我会更新我的帖子以包含任何其他详细信息。

Here are the necessary steps you should follow to add a font to your project. 以下是在项目中添加字体时应遵循的必要步骤。

  1. Install your new, custom font into Font Book. 将新的自定义字体安装到Font Book中。 I'm assuming you know how to do this and I shall leave further explanation of this step to Google. 我假设你知道如何做到这一点,我将把这一步的进一步解释留给谷歌。

  2. Next, from within Font Book, find your freshly installed font. 接下来,从Font Book中找到新安装的字体。 Select the font and click on the Information button on the Font Book toolbar. 选择字体,然后单击“字体簿”工具栏上的“ 信息”按钮。

    在此输入图像描述

  3. Take note of the PostScript name property. 记下PostScript name属性。 This must be the name of your font file (excluding the font file extension) within your application. 这必须是应用程序中字体文件的名称(不包括字体文件扩展名)。

    NOTE: I've read of some users having trouble using custom fonts with names including spaces. 注意:我读过一些用户在使用名称包含空格的自定义字体时遇到问题。 I'm not sure if this was just an error on the user's part because I'm not sure if PostScript names can even include spaces. 我不确定这只是用户的错误,因为我不确定PostScript名称是否甚至可以包含空格。 Regardless, if your PostScript name includes a space, you may still have trouble accessing your font. 无论如何,如果您的PostScript名称包含空格,您仍可能无法访问您的字体。

    If this is your problem, you may want to see if you can acquire a tool that will let you change the PostScript name property of your font. 如果这是您的问题,您可能想知道是否可以获得一个工具,可以让您更改字体的PostScript名称属性。 You will need to do this before you add the file to your XCode project. 在将文件添加到XCode项目之前,您需要执行此操作。 Otherwise, I assume that your custom font will work in the simulator, but not on your iOS device. 否则,我认为您的自定义字体将在模拟器中运行,但不会在您的iOS设备上运行。

  4. Open your XCode project and drag & drop your font, from Finder, into your project resource folder. 打开您的XCode项目,将您的字体从Finder拖放到项目资源文件夹中。 Before clicking the Finish button on the Add File screen, be sure to you check the checkbox to copy the font to the destination group's folder. 在单击“添加文件”屏幕上的“完成”按钮之前,请务必选中复选框以将字体复制到目标组的文件夹。

    在此输入图像描述

  5. After adding your font to your resources folder. 将字体添加到资源文件夹后。 Verify that the name of the font is the same as the PostScript name that was mentioned in step #3. 验证字体的名称是否与步骤3中提到的PostScript名称相同。 The name must also include any special characters such as hyphens or underscores. 名称还必须包含任何特殊字符,如连字符或下划线。

    If your font is not named the same as the PostScript name (excluding the file extension,) you will not be able to access this font from your iOS device, so rename the file in your project. 如果您的字体的名称与PostScript名称不同(不包括文件扩展名),则您将无法从iOS设备访问此字体,因此请重命名项目中的文件。 In the case of my sample font, I needed to rename NES.ttf to Nintendo-NES-Font.ttf 在我的示例字体的情况下,我需要将NES.ttf重命名为Nintendo-NES-Font.ttf

  6. Next, open the project's info.plist file. 接下来,打开项目的info.plist文件。 From the plist designer screen, right-click the first line of the plist and select Add Row . 在plist designer屏幕中,右键单击plist的第一行,然后选择Add Row This will bring up a key selection box. 这将打开一个关键选择框。 Select the "Fonts provided by application" array option. 选择“应用程序提供的字体”数组选项。

    在此输入图像描述

  7. The new array in the plist will automatically add an empty, "Item 0". plist中的新数组将自动添加一个空的“Item 0”。 Set the string property to the name of your font file. 将字符串属性设置为您的字体文件的名称。 In the case of my example, the property value should be Nintendo-NES-Font.ttf . 在我的示例中,属性值应为Nintendo-NES-Font.ttf

  8. Next, go to the Build Phases screen for your XCode project. 接下来,转到XCode项目的Build Phases屏幕。 From here, verify that your font file is in the list of Copy Bundle Resources group. 从此处,验证您的字体文件是否位于“ 复制包资源”组列表中。 If it is not, add the file in your project directory. 如果不是,请在项目目录中添加该文件。

That's it! 而已! Your font is now in your project and can be used by CClabelTTF labels. 您的字体现在在您的项目中,可以由CClabelTTF标签使用。 Remember, when you create a label, specify the font by the PostScript name. 请记住,在创建标签时,请按PostScript名称指定字体。 You do not have to include the ".ttf" file extension. 您不必包含“.tt​​f”文件扩展名。 The sample code below shows how to create a label with a custom font. 下面的示例代码显示了如何使用自定义字体创建标签。

CCLabelTTF* label = [CCLabelTTF labelWithString:@"Your label text here." 
                                       fontName:@"Nintendo-NES-Font"
                                       fontSize:16];

Add the label as a child to your layer and it will display using your custom font! 将标签作为子项添加到图层,它将使用您的自定义字体显示!

And, here is our results: 而且,这是我们的结果:

在此输入图像描述

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

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