如何添加字体到我的Cocos2D iOS项目,并使用CCLabelTTF? [英] How can I add a font to my Cocos2D iOS project and use it with a CCLabelTTF?

查看:277
本文介绍了如何添加字体到我的Cocos2D iOS项目,并使用CCLabelTTF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用自定义字体与我的Cocos2D项目。我尝试过几种技巧,但Cocos2D保持报告:

I need to use a custom font with my Cocos2D project. I've tried a few techniques, but Cocos2D keeps reporting:


无法加载字体XXX.ttf

Unable to load font XXX.ttf

为什么我无法使用自定义字体与Cocos2D一起使用?

Why can't I get my custom font to work with with Cocos2D?

这是Question + Answer帖子。这是一个问题,我有我的头撞在墙上一整天早上。在阅读了许多解决方案,在许多网站,我发现,所有可用的解决方案的汞合金是必要的,以解决这个问题。

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.

什么进一步混淆了问题一些人是,有时可以让您的应用程序工作,完美的,在模拟器,但它不会在您的iOS设备上工作!此外,我还读了一对夫妇的实例,用户有相反的问题,设备将正常工作,但不是模拟器。我没有遇到后一个问题,但我相信以下这些说明将有助于解决这个问题,为大家。

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! 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. 在Font Book中安装新的自定义字体。

  1. Install your new, custom font into Font Book. I'm assuming you know how to do this and I shall leave further explanation of this step to Google.

接下来,在Font Book中,找到您的新鲜安装字体。选择字体,然后单击字体集工具栏上的信息按钮。

Next, from within Font Book, find your freshly installed font. Select the font and click on the Information button on the Font Book toolbar.

记下 PostScript名称属性。这必须是您应用程式中的字型档案名称(不包括字型档案扩充功能)。

Take note of the PostScript name property. This must be the name of your font file (excluding the font file extension) within your application.

注意:用户在使用包含空格的名称的自定义字体时遇到问题。我不知道这是否只是用户部分的错误,因为我不知道PostScript名称是否甚至可以包括空格。无论如何,如果您的PostScript名称包含空格,您仍然可能无法访问您的字体。

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. Regardless, if your PostScript name includes a space, you may still have trouble accessing your font.

如果这是您的问题,您可能想要查看是否可以获取一个工具,让您更改字体的PostScript名称属性。在将文件添加到XCode项目之前,您需要执行此操作。否则,我假设您的自定义字体将在模拟器中工作,但不会在您的iOS设备上工作。

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. You will need to do this before you add the file to your XCode project. Otherwise, I assume that your custom font will work in the simulator, but not on your iOS device.

打开XCode项目并拖动&将您的字体,从Finder,到您的项目资源文件夹。在单击添加文件屏幕上的完成按钮之前,请务必选中复选框将字体复制到目标组的文件夹。

Open your XCode project and drag & drop your font, from Finder, into your project resource folder. 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.

>

将字体添加到资源文件夹后。验证字体的名称是否与步骤#3中提到的PostScript名称相同。名称还必须包含任何特殊字符,如连字符或下划线。

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. The name must also include any special characters such as hyphens or underscores.

如果您的字体未命名为与PostScript名称相同(不包括文件扩展名),则您将无法从iOS设备访问此字体,因此重命名您项目中的文件。在我的示例字体的情况下,我需要将 NES.ttf 重命名为 Nintendo-NES-Font.ttf

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. In the case of my sample font, I needed to rename NES.ttf to Nintendo-NES-Font.ttf

接下来,打开项目的 info.plist 文件。从plist设计器屏幕,右键单击plist的第一行,然后选择添加行。这将弹出一个键选择框。选择应用程序提供的字体数组选项。

Next, open the project's info.plist file. From the plist designer screen, right-click the first line of the plist and select Add Row. This will bring up a key selection box. Select the "Fonts provided by application" array option.

plist中的新数组将自动添加一个空的Item 0。将string属性设置为字体文件的名称。在我的示例的情况下,属性值应为 Nintendo-NES-Font.ttf

The new array in the plist will automatically add an empty, "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.

您的XCode项目的构建阶段屏幕。从此处,验证您的字体文件是否位于复制捆绑资源组列表中。如果没有,请在您的项目目录中添加该文件。

Next, go to the Build Phases screen for your XCode project. 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.

您的字体现在在您的项目中,可以由 CClabelTTF 标签使用。请记住,在创建标签时,请按PostScript名称指定字体。您不必包括.ttf文件扩展名。下面的示例代码显示了如何使用自定义字体创建标签。

That's it! Your font is now in your project and can be used by CClabelTTF labels. Remember, when you create a label, specify the font by the PostScript name. You do not have to include the ".ttf" file extension. 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!

这里是我们的结果:

这篇关于如何添加字体到我的Cocos2D iOS项目,并使用CCLabelTTF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆