自适应
1算出文本宽度,后面的文本位置加上前面的宽度
// 获取字符串长度
getStrWidth(ctx, str) {
var lineWidth = 0
for (let code of str) {
lineWidth += ctx.measureText(code).width
}
return lineWidth
},
实例
let sellPriceLength = this.getStrWidth(ctx, String(this.bean.sell_price))
ctx.fillText('¥', 65 + sellPriceLength, 760 + y)
自动换两行
效果
一行
两行
多行被切
先遍历字符串,然后如果只有一行直接返回ÿ