From b20ad1639f8d3564ed1be5afd33f602d5609e714 Mon Sep 17 00:00:00 2001 From: xiaoshumei <271583799@qq.com> Date: Wed, 8 May 2024 07:26:15 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=BB=98=E5=88=B6?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E6=97=B6=E9=81=87=E5=88=B0=E6=8D=A2=E8=A1=8C?= =?UTF-8?q?=E5=92=8C=E7=A9=BA=E8=A1=8C=E6=98=BE=E7=A4=BA=20=E9=94=99?= =?UTF-8?q?=E4=B9=B1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram_dist/index/index.js | 44 ++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/miniprogram_dist/index/index.js b/miniprogram_dist/index/index.js index 8adfe62..f300437 100644 --- a/miniprogram_dist/index/index.js +++ b/miniprogram_dist/index/index.js @@ -191,25 +191,35 @@ const handle = { // 文本宽度 大于 渲染宽度 let fillText = ''; let line = 1; - for (let i = 0; i <= text.length - 1 ; i++) { // 将文字转为数组,一行文字一个元素 - fillText = fillText + text[i]; - if (this.toRpx(this.ctx.measureText(fillText).width) >= width) { - if (line === lineNum) { - if (i !== text.length - 1) { - fillText = fillText.substring(0, fillText.length - 1) + '...'; - } - } - if(line <= lineNum) { + for (let i = 0; i <= text.length - 1 && line <= lineNum; i++) { // 将文字转为数组,一行文字一个元素 + if (text[i] === "\n") { // 遇到换行符,将现有fillText截断,单独成行。 textArr.push(fillText); - } - fillText = ''; - line++; - } else { - if(line <= lineNum) { - if(i === text.length -1){ - textArr.push(fillText); + fillText = ""; + line += 1; + } else if (text[i] === "") { // 遇到空行,调用canvas绘制一个\n + textArr.push("\n"); + fillText = ""; + line += 1; + }else { + fillText = fillText + text[i]; + if (this.toRpx(this.ctx.measureText(fillText).width) >= width) { + if (line === lineNum) { + if (i !== text.length - 1) { + fillText = fillText.substring(0, fillText.length - 1) + '...'; + } + } + if(line <= lineNum) { + textArr.push(fillText); + } + fillText = ''; + line++; + } else { + if(line <= lineNum) { + if(i === text.length -1){ + textArr.push(fillText); + } + } } - } } } textWidth = width;