页面h5适配
This commit is contained in:
@@ -1,4 +1,12 @@
|
||||
import { _decorator, Component, Label, Node, Size, UITransform } from "cc";
|
||||
import {
|
||||
_decorator,
|
||||
Component,
|
||||
Label,
|
||||
Node,
|
||||
Overflow,
|
||||
Size,
|
||||
UITransform,
|
||||
} from "cc";
|
||||
import Tools from "../../utils/tools";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@@ -11,7 +19,13 @@ export class DialogBubble extends Component {
|
||||
@property(UITransform)
|
||||
contentT: UITransform = null;
|
||||
|
||||
fixMaxWidth: number;
|
||||
init(maxWidth: number = 650) {
|
||||
this.fixMaxWidth = maxWidth;
|
||||
}
|
||||
|
||||
updateBubbleContent(str: string, callback?: (actualHeight: number) => void) {
|
||||
this.content.overflow = Overflow.NONE;
|
||||
// 设置文本内容
|
||||
this.content.string = str;
|
||||
|
||||
@@ -22,38 +36,40 @@ export class DialogBubble extends Component {
|
||||
for (const line of lines) {
|
||||
let lineWidth = 0;
|
||||
if (Tools.IsChinese(line)) {
|
||||
lineWidth = 35 * line.length;
|
||||
lineWidth = 30 * line.length;
|
||||
} else {
|
||||
lineWidth = 21 * line.length;
|
||||
lineWidth = 18 * line.length;
|
||||
}
|
||||
maxWidth = Math.max(maxWidth, lineWidth);
|
||||
}
|
||||
|
||||
if (maxWidth > this.fixMaxWidth)
|
||||
this.content.overflow = Overflow.RESIZE_HEIGHT;
|
||||
// 限制最大宽度
|
||||
const contentWidth = Math.min(maxWidth, 950);
|
||||
const contentWidth = Math.min(maxWidth, this.fixMaxWidth);
|
||||
|
||||
// 设置内容区域宽度,让Label自动计算高度
|
||||
this.contentT.setContentSize(new Size(contentWidth, 0));
|
||||
this.bg.setContentSize(new Size(contentWidth + 20, 0));
|
||||
this.bg.setContentSize(new Size(contentWidth + 17, 0));
|
||||
|
||||
// 强制更新Label的渲染数据以获取正确的高度
|
||||
this.content.updateRenderData(true);
|
||||
|
||||
// 等待一帧后获取Label的实际高度
|
||||
this.scheduleOnce(() => {
|
||||
const actualHeight =
|
||||
this.content.node.getComponent(UITransform).contentSize.height;
|
||||
const actualSize =
|
||||
this.content.node.getComponent(UITransform).contentSize;
|
||||
|
||||
// 更新内容区域的实际大小
|
||||
this.contentT.setContentSize(new Size(contentWidth, actualHeight));
|
||||
//this.contentT.setContentSize(new Size(contentWidth, actualHeight));
|
||||
|
||||
// 根据内容大小调整背景大小,添加适当的内边距
|
||||
const bgHeight = actualHeight + 20;
|
||||
this.bg.setContentSize(new Size(contentWidth + 50, bgHeight));
|
||||
|
||||
//const bgHeight = actualHeight + 17;
|
||||
this.bg.setContentSize(new Size(actualSize.x + 30, actualSize.y + 10));
|
||||
|
||||
// 回调通知实际高度
|
||||
if (callback) {
|
||||
callback(bgHeight);
|
||||
callback(actualSize.y);
|
||||
}
|
||||
}, 0);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user