Sharing info on Custom Fonts for CCLabelTTF

I couldnt find relevant information on this, so had to experiment to find this out. So thought i’ll just share it here.

If anyone is using custom fonts with CCLabelTTF, please ensure the following:

  1. Make sure you have included the .ttf into the project (not just adding it into the project folder, but actually include it in the project “Add->Existing Files”

  2. The filename has to be the same as the font FAMILY NAME. eg, I have two fonts, Rock.ttf with a font name of “Rockwell” and family name of “Rockwell”, also Rockb.ttf of font name “Rockwell Bold” and a family name of “Rockwell” (you can check the info using windows font viewer). The first file had to be renamed “Rockwell.ttf” for it to work, but the second one needs to be edited because they share the same family name, so changing it to “Rockwell Bold.ttf” will not work unless you use some software to edit the font family name to “Rockwell Bold” as well.

The above applies to development on Win8 RTM for Win8 Metro app. May or may not apply to other versions (untested)

How do you do this on Mac? I am using Arial for my TTF Labels and the weird thing is, it works perfectly fine on iPhone but not on iPad. :s

@LanceGray
sorry, this finding was for win8 metro app development. I have not test it on other versions or platform. If you install your font on mac, view the font using Font Book. Go to the detailed view of the font and look for the different names given to that font. Try using the Postscript name (should be the first name on the detailed list) instead of the family name, as I know cocos2d-iphone v2.0 uses that for custom font.

Thanks for sharing this!

I was wrapping around my head about why my fonts didn’t work in Windows 8 (even though the name is the same as the font NAME)… so it has to be the family name huh.

Was looking at this the to try to get it working for my game on Mac OS X, not sure if any one will find it useful but here is what I did:

  1. Add the following entry to your info.plist

     ATSApplicationFontsPath
     .
    

or replace the . with the path of the folder where you keep your fonts, for more info check this out http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/GeneralPurposeKeys.html

  1. Make this code change to cocos2dx/platform/mac/CCImage.mm in the method @ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAlign, const char * pFontName, int nSize, tImageInfo* pInfo, cocos2d::ccColor3B* pStrokeColor) @

    if (font == nil) {
    font = [[NSFontManager sharedFontManager]
    fontWithFamily:@“Arial”
    traits:NSUnboldFontMask | NSUnitalicFontMask
    weight:0
    size:nSize];
    }

becomes:

    if (font == nil) {
        font = [NSFont fontWithName:[NSString stringWithUTF8String:pFontName] size:nSize];
        if (font == nil) {
            font = [[NSFontManager sharedFontManager]
                fontWithFamily:@"Arial"
                traits:NSUnboldFontMask | NSUnitalicFontMask
                weight:0
                size:nSize];
        }
    }
  1. if you have a gard @ #if (CC_TARGET_PLATFORM CC_PLATFORM_IOS) @ around your font names change it to @ #if (CC_TARGET_PLATFORM CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM CC_PLATFORM_MAC) @

That should be it… the names that you are using for iOS should work on mac too…

If you do not know what font name you should use, add this bit of code that will list all the font names available; it will slow your code right down if there are tons of fonts that have not been found, but once you find what you are looking for you can take it out :wink:

<pre>
if (font nil) {
for (NSString *name in [[NSFontManager sharedFontManager] availableFonts]) {
NSLog("%", name);
}
}

Edmund Ching wrote:

I couldnt find relevant information on this, so had to experiment to find this out. So thought i’ll just share it here.
>
If anyone is using custom fonts with CCLabelTTF, please ensure the following:
>

  1. Make sure you have included the .ttf into the project (not just adding it into the project folder, but actually include it in the project “Add->Existing Files”
    >
  2. The filename has to be the same as the font FAMILY NAME. eg, I have two fonts, Rock.ttf with a font name of “Rockwell” and family name of “Rockwell”, also Rockb.ttf of font name “Rockwell Bold” and a family name of “Rockwell” (you can check the info using windows font viewer). The first file had to be renamed “Rockwell.ttf” for it to work, but the second one needs to be edited because they share the same family name, so changing it to “Rockwell Bold.ttf” will not work unless you use some software to edit the font family name to “Rockwell Bold” as well.
    >
    The above applies to development on Win8 RTM for Win8 Metro app. May or may not apply to other versions (untested)

Hi Edmund, I have been trying to add custom fonts with CCLabelttf to my app on Windows 8 RTM, I have tried your proposed solution. I have font named as Gingar-DemiBold.ttf. I checked out its font properties using a tool names as font lab studio. The full name was Gingar-DemiBold and the Family name was Gingar DemiBold. So I made full name and Family name the same as Gingar-DemiBold and renamed the file as Gingar-DemiBold.ttf. But still it is not displaying the correct font.

Alternatively I tried out a font “A Damn mess.ttf” from the Cocos2dx Font test. The file read perfectly and the correct font displayed on the screen. When I checked out its font properties as I mentioned all the names like PS-name, fullName, familyName and each other attribute had the same name as “A Damn Mess”. Do I need to change all the names of the font same. I have have attached the files. Can you suggest me a better tool to edit custom font attributes.

Same question.
I can not use custom ttf font on windows also.
Thanks all who will fix it.

This is due to a bug in CCImage::setFont. See this thread for more info: http://www.cocos2d-x.org/forums/6/topics/32189

I made a pull request from Brian Ramage’s fix on github: https://github.com/cocos2d/cocos2d-x/pull/4365