如何将字体添加到我的 Cocos2D iOS 项目并与 CCLabelTTF 一起使用?

How can I add a font to my Cocos2D iOS project and use it with a CCLabelTTF?(如何将字体添加到我的 Cocos2D iOS 项目并与 CCLabelTTF 一起使用?)

本文介绍了如何将字体添加到我的 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?

这是一个问题+答案的帖子.这是一个让我整个早上都用头撞墙的问题.在阅读了众多网站上的众多解决方案后,我发现需要综合所有可用的解决方案来解决此问题.

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. 将新的自定义字体安装到字体册中.我假设您知道如何执行此操作,我会将对此步骤的进一步解释留给 Google.

  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 的第一行并选择 Add Row.这将弹出一个键选择框.选择应用程序提供的字体"数组选项.

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".将字符串属性设置为字体文件的名称.在我的例子中,属性值应该是 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 项目的 Build Phases 屏幕.从这里,验证您的字体文件是否在 Copy Bundle Resources 组的列表中.如果不是,请将该文件添加到您的项目目录中.

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 名称指定字体.您不必包含.tt​​f"文件扩展名.下面的示例代码展示了如何使用自定义字体创建标签.

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!

而且,这是我们的结果:

And, here is our results:

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

本文标题为:如何将字体添加到我的 Cocos2D iOS 项目并与 CCLabelTTF 一起使用?

基础教程推荐