Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using custom fonts in Cocos2d

How do I use custom TTF fonts in my Cocos2d iPhone apps?

like image 577
Hanaan Rosenthal Avatar asked Nov 19 '10 19:11

Hanaan Rosenthal


2 Answers

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. alt text

  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"

alt text

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>
like image 180
Hanaan Rosenthal Avatar answered Oct 16 '22 00:10

Hanaan Rosenthal


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.

like image 30
Paul Gregory Avatar answered Oct 16 '22 00:10

Paul Gregory