如何获取任意字符串的宽度

image 如何计算字符所占宽度

export function getTextWith(text, fontSize) {

const font = `${fontSize}pt arial`

const canvas = document.createElement("canvas");

const context = canvas.getContext("2d");

context.font = font;

return context.measureText(text).width;

} 比实际的偏大点,凑合吧

1赞

label.string = data.content;
label._forceUpdateRenderData();
console.log(label.node.width);