uni-app canvas单行文本溢出隐藏
//canvas单行文本自动省略
fittingString(_ctx, str, maxWidth) {
let strWidth = _ctx.measureText(str).width;
const ellipsis = '…';
const ellipsisWidth = _ctx.measureText(ellipsis).width;
if (strWidth <= maxWidth || maxWidth <= ellipsisWidth) {
return str;
} else {
var len = str.length;
while (strWidth >= maxWidth - ellipsisWidth && len-- > 0) {
str = str.slice(0, len);
strWidth = _ctx.measureText(str).width;
}
return str + ellipsis;
}
}
本文标题:uni-app canvas单行文本溢出隐藏
本文链接:https://www.haomeiwen.com/subject/mcbelltx.html
网友评论