简体   繁体   中英

Cannot use custom font in Cocos2d-x project

This code works:

auto label = LabelTTF::create(CokeStore::name, "Marker Felt", 170);

But none of these work:

auto label = LabelTTF::create(CokeStore::name, "Coca Cola ii", 170);

auto label = LabelTTF::create(CokeStore::name, "CocaColaii", 170);

auto label = LabelTTF::create(CokeStore::name, "cocacolaii", 170);

I imported all the fonts as resources into my project and added fonts' path into Fonts provided by application of Info.plist . I'm using Cocos2d-x v. 3 , the latest version from Github.

Anybody can help me with this? Is there anything wrong with my code and how to fix them? Thanks for your help!

I was facing alot of difficulty adding a custom font. I know the question has already been answered but I just want to share what solved the problem for me. These are the points you need to ensure to use a custom font:

  1. Font name must match the PostScript name of that font (you can easily find the postscript name using the Font Book, just select the font and press command+i) , so your font name should be: PostScriptName.ttf

  2. In the Info.plist of your project, make sure to add the font name under "Fonts provided by application", just put in the font name and extension for item0 or whatever the length of your font stack is: PostScriptName.ttf, Now, the next step that solved the issue for me was:

  3. Ensure that the font file exists in the "Copy Bundle Resources" Section of the 'Build Phases' settings for your project. For me it somehow wasnt there and thats why the font wasnt loading, so I just manually added it.

  4. Now you can use it even with LabelTTF::create("Text", "PostScriptName", 24)!

In Cocos2d-x V3, there is a new class Label .

I tried this and it worked:

Label *label = Label::createWithTTF("Your string here","cocacolaii.ttf", 170);

For more details, you can read this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM