开发者

Using custom fonts in Cocos2d

How do I use custom TTF fon开发者_开发问答ts in my Cocos2d iPhone apps?


I searched for this for a while and decided to post as question and answer for people wanting to implement.

The solution is quite easy.

  1. Find the fonts you want and download them. This website has a huge collection of free fonts.

  2. Add the font files to your project.

    Using custom fonts in Cocos2d

  3. Add the font names to your info.plist file using the array below as an example.

  4. Find the font's name; Double click the font file and use the font name shown in the title of the window. In this example it is "Action Man"

Using custom fonts in Cocos2d

To use the font name the way you would normally:

CCLabel* myLabel = [CCLabel labelWithString:@"Some Text" fontName:@"Action Man" fontSize:18];

Add this to your info.plist file:

<key>UIAppFonts</key>
<array>
    <string>Action Man Bold.ttf</string>
    <string>AdineKirnberg-S.ttf</string>
</array>


In Cocos2D, you can include the font file with your resources and then in fontName: simply supply the filename. For example:

CCLabelTTF *label = [CCLabelTTF labelWithString:@"whatever" fontName:@"FONTNAME.ttf" fontSize:18];

This is often easier than the method Hanaan has posted.

You should use the exact same file name capitalisation as the filename.

You do not need to add fonts to your info.plist if you are only using it in Cocos2D bits.

If, like Confused, you find that the font works on the simulator but not the device, it is probably a file name capitalisation mistake. The iPhone is case-sensitive, the simulator is not.


As these answers are outdated, I'm sure my question/ answer will help all those out there.

Importing fonts for cocos2dx iOS

this is for iOS and Android platform font importing, with just a single line of code!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜