1

I've referred to this link: Using custom fonts in Cocos2d

and this link : http://captainjor.wordpress.com/2013/04/03/cocos2d-x-custom-ttf-font/

The first link works but I can only specify the family name of the font (Name based on the top of the font window). If I wanted Bold, or some crazy ass styling, I cannot specify explicitly as I opened other styles fonts like BoldItalic or Italic, it still returns me the Family Name.

The second link does not work at all.

The code I've used in Cocos2dx that works:

CCLabelTTF *energyLabel = CCLabelTTF::create("ABCDEFGHIJKL", "Roboto", 60.0f); 

This one doesn't:

CCLabelTTF *energyLabel = CCLabelTTF::create("ABCDEFGHIJKL", "Roboto Black Italic", 60.0f); 

Note that I've already double checked in my Info.plist file to import all the required fonts. Anyone has any more solutions? =/ I'm pretty stumped. Importing fonts are such PITA.

EDIT: I've stumbled again on this issue, but in a different approach. just thought I would post here a few important pointers on how I exactly do font importing for Android and iOS. Note that this is best for supporting the Import of fonts with ONE line of code.

  1. First, I put all my font files in the Resources Folder (Not in any subfolders, just at the root!)
  2. Specifically for iOS, you need to import the font files by declaring them in your plist. How? Like this:

font declaration list in iOS

-> Yes we can see that I have somehow included the "fonts/" subfolder directory EVEN THOUGH the fonts are at the ROOT folder in Resources. This is the weirdest part here and this should be only thing that's tricky. I tried removing the "fonts/" subfolder directory reference and it DOESN'T WORK when I tried to use the font to display texts/ labels in my application

And this is the line that works for both iOS and Android:

CCLabelTTF* levelLabel = CCLabelTTF::create("TextDisplayed", Roboto-BlackItalic.ttf, 12); 

This line of code heavily relies on this fact: PostScript Name of a Font needs to be exactly the same as file name.

1
  • 1
    Note that this is out of date for the recent versions of cocos2d-x. Commented Jul 10, 2014 at 9:43

2 Answers 2

6

Would you try using

"Roboto-BlackItalic" 

as a font name? I have just checked in some code base of mine and I am using the font filename (without extension) rather that the proper font name and it works just fine.

EDIT: as per Joel Tay's comment, it is the Postscript font name the right one to use. You can find it in Font Book, as the attached picture shows, by selecting Show Font Info from the Preview Menu.

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Surprising it works.. Can I know how do you get that name? Is there a certain property in a font file I'm not seeing? I clicked "Get Info" on the .ttf file and all I got is the Full Name which is Roboto Black Italic
that's simply the file name... it works like that for external fonts. btw, don't forget to accept the answer!
Alright I got it, turns out the importing is fine. You'll need to use the FontBook to actually get the PostScript Name. Since I'm developing for iOS and Android, to maintain compatibility and messed up name schemes, I named the fonts the exact name as the PostScript name. Thanks for the pointer though!
@JoelTay: great hint! I added it to my answer for future reference.
0

You can check my answer here.-

UIFont custom font not displaying on device

To display all 'real' font names visible by your application.

1 Comment

It would be a hassle to actually keep checking the real font names by constantly checking the array. But it's not wrong though. I managed to 'bypass' this by just using the FontBook app :) Just press GetInfo on the specific style of font and the PostScript name is displayed for us to use :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.