Files
18xchat/assets/Scripts/test/DialogBubble.ts
T
2025-07-22 16:43:10 +08:00

58 lines
1.4 KiB
TypeScript

import { _decorator, Component, Label, Node, Size, UITransform } from "cc";
import Tools from "./tools";
const { ccclass, property } = _decorator;
@ccclass("DialogBubble")
export class DialogBubble extends Component {
@property(UITransform)
bg: UITransform = null;
@property(Label)
content: Label = null;
@property(UITransform)
contentT: UITransform = null;
updateBubbleContent(str: string) {
const lines = str.split("\n");
let len = 0;
let index = 0;
for (let i = 0; i < lines.length; i++) {
const element = lines[i];
if (element.length > len) {
len = element.length;
index = i;
}
}
let finalLen = 0;
// for (let i = 0; i < lines[index].length; i++) {
// const element = lines[index][i];
// if ("\u4e00" <= element[i] && element[i] <= "\u9fff") {
// finalLen += 35;
// } else {
// finalLen += 18;
// }
// }
if (Tools.IsChinese(lines[index])) {
finalLen = 35 * lines[index].length;
} else {
finalLen = 21 * lines[index].length;
}
this.content.string = str;
this.contentT.setContentSize(
new Size(Math.min(finalLen, 950), this.contentT.contentSize.y)
);
this.content.updateRenderData();
this.bg.setContentSize(
new Size(
this.contentT.contentSize.x + 30,
this.contentT.contentSize.y + 20
)
);
return this.bg.contentSize.y;
}
}