一、环境搭建

1.创建一个 Text UI组件

Laya学习笔记-09使用ttf字体_环境搭建

2.设置 Text 的宽高及位置

Laya学习笔记-09使用ttf字体_环境搭建_02

3.将ttf字体拖入到 工程->Assets 目录下

Laya学习笔记-09使用ttf字体_加载_03

 

二、代码加载ttf字体

1.创建一个脚本命名为 “TTFCtrl.js” ,代码如下

export default class TTFCtrl extends Laya.Script {

constructor() {
super();
/** @prop {name:txt, tips:"提示文本", type:Node, default:null}*/
this.txt=null;
}

onAwake() {

// Laya.loader.load("BalooBhaina-Regular.ttf",Laya.Handler.create(this,Function(res){

// }),null,Laya.Loader.TTF,0,true);

Laya.loader.load("BalooBhaina-Regular.ttf",Laya.Handler.create(this,function(res){

this.txt.font=res.fontName;
console.log(res.fontName);

}),null,Laya.Loader.TTF,0,true);

}
}

2.将“TTFCtrl.js” 脚本挂载到Scenes上,并且指定属性。大功告成!

Laya学习笔记-09使用ttf字体_加载_04