最新官方文档太老了
http://developer.egret.com/cn/github/egret-docs/Engine2D/textField/textStyle/index.html
还在使用egret.registerFontMapping(),这个API早就不支持了。
官方论坛上也有多个帖子在询问
https://bbs.egret.com/forum.php?mod=viewthread&tid=54983&highlight=%E5%AD%97%E4%BD%93
https://bbs.egret.com/forum.php?mod=viewthread&tid=52751&highlight=%E5%AD%97%E4%BD%93
经过多次来回试验,终于找到可行的解决方案。在下列版本上,用小米,锤子,oppo的多款手机测试通过
引擎版本: 5.2.16 ~5.2.20(当前最新版本)
native android版本: 0.1.16和0.1.17(当前最新版本)
先概括一下:在web和native两个平台上,支持外部字体的方法不同。
我们把要使用的字体放到resource目录下, 比如名字是:testFont.TTF
1、在web上支持的方法。修改index文件,增加一个一种字体:
<style>
html, body {
-ms-touch-action: none;
background: #000000;
padding: 0;
border: 0;
margin: 0;
height: 100%;
}
</style>
<style type="text/css"> /* 在默认的style后面加一个字体style就可以了。 */
@font-face {
font-family: 'Saira SemiCondensed';
src:url('./resource/testFont.TTF') format('truetype');
font-weight: normal;
font-style: normal;
}
div
{
font-family: "Saira SemiCondensed";
}
</style>
然后在引擎的label上,用fontFamily="Saira SemiCondensed", 就可以使用这个字体了.。可以在exml中直接定义,很方便。
2、在native android 上支持的方法。
在label,使用代码如下:
let myLabel: eui.Label
myLabe.fontFamily = "resource/testFont.TTF";
3、把上面两个方案结合起来。
上面这两种方案是不兼容的。native的方案直接用浏览器访问,字体会非常小,怪异。加上判断即可。注意判断native版本,要使用 egret.RuntimeType.RUNTIME2,而不是egret.RuntimeType.NATIVE
let myLabel: eui.Label
if (egret.Capabilities.runtimeType == egret.RuntimeType.RUNTIME2) {
myLabel.fontFamily = "resource/testFont.TTF";
}