Cocos2d-js:自定义TTF字体不会在Android中显示

Mic*_*ael 1 truetype cocos2d-js

我定义了字体资源

var res = {
   indieflower_ttf: {type:"font", name:"IndieFlower", srcs:["res/fonts/IndieFlower.ttf"]},
}
Run Code Online (Sandbox Code Playgroud)

并创建一个TTF标签:

var text = cc.LabelTTF.create(text, "IndieFlower", bubble.fontsize, cc.size(60,0),cc.TEXT_ALIGNMENT_LEFT, cc.VERTICAL_TEXT_ALIGNMENT_TOP);
Run Code Online (Sandbox Code Playgroud)

并且在Firefox和Chrome中显示正常,但会在Android上显示默认字体(Arial).

还有什么要做的?我检查了线程Cocos2d-js:如何在Android设备上使用自定义ttf字体?,但它没有帮助.

Mic*_*ael 7

问题是,本机平台需要ttf字体的完整路径,而html平台需要字体名称.我从cocos2d-x论坛得到了两个解决方案:

  1. 使用位图字体.(是的......但我想要ttf)

  2. 编码:

在我现在的资源文件中:

var res = {
    indieflower_ttf: {type:"font", name:"IndieFlower", srcs:["res/fonts/IndieFlower.ttf"]},
}
Run Code Online (Sandbox Code Playgroud)

然后是辅助函数:

var _b_getFontName = function(resource) {
    if (cc.sys.isNative) {
        return resource.srcs[0];
    } else {
        return resource.name;
    }
}
Run Code Online (Sandbox Code Playgroud)

并创建一个标签:

var text = cc.LabelTTF.create("text", _b_getFontName(res.indieflower_ttf), 48, cc.size(300,0),cc.TEXT_ALIGNMENT_CENTER, cc.VERTICAL_TEXT_ALIGNMENT_TOP);
Run Code Online (Sandbox Code Playgroud)