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

2021-04-16  本文已影响0人  hao_developer
//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;
      }
    }
上一篇下一篇

猜你喜欢

热点阅读