Whatever I do, I somehow always end up using TTF labels over any other kind of labels. There are many advantages of using a BMFont over a TTF one, but as a developer it’s always easier for me to just drop in a TTF file and be done with it.

Although I searched, I couldn’t find a straightforward, documented way to handle custom TTF fonts seamlessly on both Android and iOS, so here’s what I ended up doing.

iOS

First, you’ll need to integrate the TTF file into the iOS build. To do this, drag and drop it into Xcode into the Resources folder.

Then edit the info.plist file and locate the “Fonts provided by application” row.

If you can’t find it add a new row and start typing in the name, Xcode will autocomplete it. Make sure the row type is “Array”. Then add a new row within this array, with the String type.

Type in the name of the ttf file. If your font is called “My Awesome font.ttf” then that’s what you’re putting there.

Android

The TTF files will be automatically copied by build_native.sh to the Android assets folder so there’s nothing to do here.

Using the fonts

iOS references the fonts based on their PostScript name. You can find it by opening the font in the Font Book application (included in OSX), select the font and there you have it: 00000219

However, on Android you’ll have to reference the files based on the filename so now we have a mismatch between the two. How do we handle this?

I maintain a Defines.h file that contains a some #define statements that I use to make life easier. I include this file from most of the other files that I use. Using preprocessor macros you can define a value for a constant for the Android build and a value for the iOS build:

// fonts
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
#define kDefaultFont		"MyAwesomeFont-Regular"
#define kMenuFont			"MyOtherAwesomeFont"
#endif
 
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#define kDefaultFont		"My Awesome font.ttf"
#define kMenuFont			"My Other Awesome font.ttf"
#endif

When you want to instantiate a CCLabelTTF and you want to reference a font, you will call:

CCLabelTTF *playLabel = CCLabelTTF::create("Start", kMenuFont, 30);

This also helps when you want to just drop in a new font give it a run and see what it looks like, or you want to replace a font everywhere you use it- you will no longer have to search for it, you just edit the macro definition and you’re done!

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *

Email
Print