What is the correct way to use a custom font in IOS?

Hello,

I have been trying to use a custom font[1] in my game and I have managed to use it in Android and web but not in IOS.
I have been searching through the forums and although I have found some mentions [2][3][4] I can not make it work.

I have my xcode project like this:

But I have tried writing only Soup of Justice in xcode but that did not work.

In my cocos studio project I have in resource.js:

font: ‘res/Fonts/Soup of Justice.ttf’ and it’s preloaded in the main.js

Then my calls of cc.LabelTTF :
var label = new cc.LabelTTF(“Prueba” , “Soup of Justice”, 50);
but i have also tried
var label = new cc.LabelTTF(“Prueba” , ‘res/Fonts/Soup of Justice.ttf’, 50);

Any help would be welcome!

[1] http://www.dafont.com/es/soup-of-justice.font
[2] [Bug?] Problem loading CCLabelTTF custom font on IOS
[3] Cant use Custom .ttf fonts in v 2.1.5 / 2.2.1 / 3.0
[4] http://www.cocos2d-x.org/wiki/How_to_Use_Custom_TTF_Font_on_iOS

1 Like

I have this problem too. (cocos2djs 3.5)
js-tests project has this problem and i cant find a solution.

If i use ccui.button with a custom font, it works! With cc.LabelTTF it fails.

I was going to write a post whenever I had time explaing how to do it properly but the fast solution is:

-I checked if the font was properly added to the project following this instruccions:

http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

Then with this code:

for (NSString* family in [UIFont familyNames])
{
NSLog(@"%@", family);

for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
    NSLog(@"  %@", name);
}

}

I found the real name of my font that it was soupofjustice.

Then I have in my project ios and in my project of the cocos ide the font added in resources. (I think in cocos ide it’s not needed but I am targeting web also so I have to preload it, still I have not tried without this so I mention it just in case).

Then in my main I have something like this:

var font = 'FontCrossy';
if (cc.sys.isNative) {
    if(cc.sys.os == cc.sys.OS_ANDROID) {
        font = 'res/Fonts/Soup of Justice.ttf';
    }
    else {
        font = 'SoupofJustice';
    }
}
csky.global_constants.FONT = font;

And for the cc.LabelTTF:

    var label = new cc.LabelTTF("text",csky.global_constants.FONT, 50);

Hope it helps

1 Like

Thanks! @alexgg