import Text = Laya.Text;
import Event = Laya.Event;
export default class TextAdaptive extends Text {
private _startFontSize: number = 0;
constructor() {
super();
}
onAwake(): void {
this._startFontSize = this.fontSize;
}
onEnable(): void {
this.on(Event.CHANGE, this, this.onChange);
}
private onChange(): void {
if(this.fontSize != this._startFontSize) {
this.fontSize = this._startFontSize;
}
if(this.textWidth > this.width) {
this.fontSize = (this.fontSize * (this.width / this.textWidth)) | 0;
}
}
onDisable(): void {
this.off(Event.CHANGE, this, this.onChange);
}
}
将该类拖入到对应text的组件的runtime中即可生效,该类适用于文本内容可能超出设置的文本边界情况时。