Fonts not working on android device

Hey All,

I am using TTF fonts and its working fine on iOS devices but its not working on android devices. Font name and file name is both RockoUltraFLF and I have placed it in resources folder.

try setting the name of the font to fonts/yourfontname.ttf, and put your font under fonts folder under resources
create the fonts folder if not there

example:

    CCLabelTTF *item1Text =  CCLabelTTF::create("Info", "fonts/atarist.ttf", 10.0);

You know what I am facing very strange issues.
I have my font in resources folder (no sub folder), I have added my font in info.plist as “fonts/RockoUltraFLF.ttf” and I am using it in my code as CCLabelTTF::Create(“text”,“RockoUltraFLF”,10);

Lolz what is happening and Its working fine only on IOS but not on android.

Your’re doing it wrong. Only iOS/Mac search fonts in Info.plist and can use font name instead of path to font file. On other systems, you should use relative path to you font file, starting from resource folder as root. I.e if you have no subfolders, just use “RockoUltraFLF.ttf”

CCLabelTTF *item1Text =  CCLabelTTF::create("Info", "RockoUltraFLF.ttf", 10.0);

P.S. Prefer Factory pattern over macro defines and copy&paste. For example, you can create class CCLabelTTFUtils which have static method

static CCLabelTTF *create(const std::string &text, float fontSize).

Guess what

I placed the font file in fonts folder and removed entry from info.plist , I used in code as “fonts/RockoUltraFLF.ttf” and Its now working on android but not on iOS. I guess we need to make android build first and then change code to work in iOS and then make iOS build.

but I wonder why iOS part isn’t working. Adding font entry in info.plist is required i think, other wise it wont work on iOS.

Thanks for the help

allow me to explain

I checked the source code of cocos2dx a few months ago(cant remember which class it was),
the only reason why it works in ios is, it looks for it under resources
where as when android is detected it will look for it under “fonts/”, it is harcoded in the sourcecode

so place your font under both folders and should work

thats all

In android the font name goes like this - “fonts/FontName.ttf” for iOS its just “fontName”. Use appropriate macros or follow the method used in the Tests project. There is no need to change the code everytime.

regards,
Raghu S

metin lapo wrote:

where as when android is detected it will look for it under “fonts/”, it is harcoded in the sourcecode
No, it doesn’t hardcoded.

Thanks alot guys, with discussion here I finally managed to figure out the solution and now I understand why fonts can cause low memory since it has to be used as a static array of fonts with device platform check.:slight_smile:

Right, it’s just a name of the relative path, you could name it whatever else rather than ‘fonts/’.

Sergey Shambir wrote:

metin lapo wrote:
> where as when android is detected it will look for it under “fonts/”, it is harcoded in the sourcecode
No, it doesn’t hardcoded.

Mehroze Yaqoob wrote:

Hey All,
>
I am using TTF fonts and its working fine on iOS devices but its not working on android devices. Font name and file name is both RockoUltraFLF and I have placed it in resources folder.

remove the .ttf extension in case of android for the file name in the code. You then do not to give a folder reference like “font/abc.ttf”, simply pass “abc” as a font name. I have done in my 2-3 games and it works 100%.

CCLableTTF * font = CCLabelTTF::create(“abc”, “Helvetica”, 20);

James Chen wrote:

Right, it’s just a name of the relative path, you could name it whatever else rather than ‘fonts/’.
>
Sergey Shambir wrote:
> metin lapo wrote:
> > where as when android is detected it will look for it under “fonts/”, it is harcoded in the sourcecode
> No, it doesn’t hardcoded.

hi James Chen,

When I use SystemFont on ios like “Helvetica-Bold.ttf” it works fine._
CCLabelTTF * font = CCLabelTTF::create(“anything”, “Helvetica-Bold.ttf”, 20); <— works fine on ios_

but when i do samething for android it does not work.

Previously if I use system font as a regular like “Helvetica.ttf” it works on ios and on android in a different way

CCLabelTTF * font = CCLabelTTF::create; <—- on ios works well
CCLabelTTF * font = CCLabelTTF::create; <—- on android it works in this way
CCLabelTTF * font = CCLabelTTF::create; <—- on ios works well
CCLabelTTF * font = CCLabelTTF::create; <—- on android not works

The last example is not working any idea

“Helvetica” seems to be a system font on android. So you don’t need to add .ttf probably, while “Helvetica-Bold” is not a system font, therefore, it can’t work on android.
If you put .ttf files to the root of resource path.

CCLabelTTF * font = CCLabelTTF::create("anything", "Helvetica.ttf", 20);

should both work for android and iOS. On iOS, you have to do one thing more, that’s adding Helvetica.ttf to info.plist.

1 Like

Refer to this if anyone’s interested. I’ve written a simple and rough guide!
[[http://stackoverflow.com/questions/19153659/importing-fonts-for-cocos2dx-ios]]

@quiet_readonly
I don’t have the font in plist and I’m trying to use like this: “fonts/smallboldpixel.ttf” but it’s not working. I tried also to add in plist file with/withouth relative path “fonts/smallboldpixel.ttf” but nothing. “fonts” folder it’s at the same level with “Resources”.
Can somebody help me pls?
Regards

Your structure should be like this :
Resources
–fonts
----“YourFontName.ttf”

Now you need to add this to your Info.plist with the relative path. i . e. “fonts/YourFontName.ttf”
When you want to use it with iOS use only “YourFontName”. Notice the name without extension.
When you want to use it with Android use “fonts/YourFontName.ttf”.

1 Like

This might be out of date, but my Android devices (I have two) do not have Helvetica… I always get LOTS of these warnings in console when running Android:

D/cocos2d-x debug info(22548): cocos2d: fullPathForFilename: No file found at Helvetica. Possible missing file.

Now I don’t specifically use Helvetica, I use other actual fonts so this is not a problem ,except the logs are CRAMMEd with those warnings… because the Label:s in cocos2d-x are hardcoded to Helvetica until you change them?

1 Like

+1 Cocos tries unsuccessfully to load “Helvetica” as the system font every time a system font label is created. It also fails to load “sans-serif” as a system font name. Probably should be configurable in a manifest file which font name is used for Android system font, or expose an API to allow us to specify programatically, similar to setting the sprite search paths.

for those who want only hide the log, there is this trick - but it will hide also others FileUtils log.
FileUtils::getInstance()->setPopupNotify(false);