20

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

Ivan Mir
  • 1,209
  • 1
  • 12
  • 32
Hanaan Rosenthal
  • 1,359
  • 1
  • 11
  • 20
  • I have done all of this and cannot get it to work on the device, ONLY on the simulator! How do I get it to work on the device? –  Dec 31 '10 at 15:17
  • I've posted a Q&A that solves most of the pitfalls that arise when using a custom font in cocos2d. Check it out if you are having trouble adding custom fonts to your project. (http://stackoverflow.com/questions/14508675/how-can-i-add-a-font-to-my-cocos2d-ios-project-and-use-it-with-a-cclabelttf) – RLH Jan 25 '13 at 12:14

3 Answers3

54

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>
Hanaan Rosenthal
  • 1,359
  • 1
  • 11
  • 20
18

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.

Paul Gregory
  • 1,733
  • 19
  • 25
  • 7
    This is not the case in cocos2d 2.0, CCLabelTTF will only use UIFont to resolve the font it wants. This requires adding the TTF to the Info.plist. – bshirley Jun 09 '12 at 17:25
  • @bshirley, me also facing same problem in cocos2d 2.0, tell me where to add ttf font file name in plist. – Guru Jul 07 '12 at 15:20
  • 1
    @Raj - answer is given above. Add TTF to the project as a resource. Add the font file to the info.plist, key UIAppFonts which is an array with string values (i.e. "FooBar.ttf"). Make sure you use the FONT NAME in the code, and not the file name. Select and inspect the font in Mac OS to glean the actual font name which is encoded within the file. (which might be "Foo Bar BOLD") – bshirley Jul 10 '12 at 18:42
  • @bshirley, not working Sir. here is my ttf file http://is.gd/9DHOpe and plist http://is.gd/DTaTfz and code CCLabelTTF *gameOverLabel = [CCLabelTTF labelWithString:@"GAME OVER" fontName:@"Game Over Regular" fontSize:fntSize]; – Guru Jul 11 '12 at 04:38
  • And game_over.ttf is included as a resource of the project? Don't know your problem. I'd chase it in the debugger into the cocos2d code to see if that helps you out. – bshirley Jul 12 '12 at 21:15
0

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!

Community
  • 1
  • 1
Joel Tay
  • 83
  • 1
  • 9