我的思路是 先设置文本为自动大小,获取文本长度textWidth,通过textWidth/uiWidth求得行数,再乘以单行文本高度 就得到了 这个文本的总宽高。
setText(strId) {
this.txt_Question.autoSizeEnable = true;
this.txt_Question.textHorizontalLayout = UI.UITextHorizontalLayout.Clipping;
this.txt_Question.text = Lanstr(strId, `errorId:${strId}`);
setTimeout(() => {
if (this.txt_Question.size.x > this._maxWidth) {
this._tmpPos.x = this._maxWidth;
this._tmpPos.y = Math.ceil(this.txt_Question.size.x / this._maxWidth) * this.txt_Question.textSingleHeight;
this.txt_Question.autoSizeEnable = false;
this.txt_Question.textHorizontalLayout = UI.UITextHorizontalLayout.AutoWarpText;
this.txt_Question.size = this._tmpPos;
} else {
this._tmpPos.x = this.txt_Question.size.x; //直接赋值会变成 一个引用
this._tmpPos.y = this.txt_Question.size.y;
}
this._tmpPos.x += this._paddingX * 2;
this._tmpPos.y += this._paddingY * 2;
this.img_qBG.size = this._tmpPos;
this.btn_Question.size = this._tmpPos;
this.uiObject.size = this._tmpPos;
this._tmpPos.x = this._paddingX;
this._tmpPos.y = this._paddingY;
this.txt_Question.position = this._tmpPos;
}, 1);
}
但就不是很准确,如图所示 比如刚好三行多一两个字 算出来还是三行
|