美文网首页
uni-app canvas单行文本溢出隐藏

uni-app canvas单行文本溢出隐藏

作者: hao_developer | 来源:发表于2021-04-16 16:37 被阅读0次
//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