From 24311ab1f63112f1bbbdadb4818b56a4f77c0eb6 Mon Sep 17 00:00:00 2001 From: CPPAlien Date: Tue, 17 Jul 2018 17:32:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dwidth=E5=B0=8F=E4=BA=8E?= =?UTF-8?q?=E5=AD=97=E4=BD=93=E5=A4=A7=E5=B0=8F=E6=97=B6=E9=99=B7=E5=85=A5?= =?UTF-8?q?=E6=AD=BB=E5=BE=AA=E7=8E=AF=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pen.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/pen.js b/lib/pen.js index 712c0eb..ff29075 100644 --- a/lib/pen.js +++ b/lib/pen.js @@ -104,8 +104,8 @@ export default class Painter { let extra; if (view.type === 'text') { const fontWeight = view.css.fontWeight === 'bold' ? 'bold' : 'normal'; - const fontSize = view.css.fontSize ? view.css.fontSize : '20rpx'; - this.ctx.font = `normal ${fontWeight} ${fontSize.toPx()}px sans-serif`; + view.css.fontSize = view.css.fontSize ? view.css.fontSize : '20rpx'; + this.ctx.font = `normal ${fontWeight} ${view.css.fontSize.toPx()}px sans-serif`; this.ctx.setFillStyle(view.css.color ? view.css.color : 'black'); // this.ctx.setFontSize(view.css.fontSize.toPx()); const textLength = this.ctx.measureText(view.text).width; @@ -197,6 +197,10 @@ export default class Painter { if (measuredWith < width) { text = view.text.substr(start, ++alreadyCount); } else { + if (text.length <= 1) { + // 如果只有一个字符时,直接跳出循环 + break; + } text = view.text.substr(start, --alreadyCount); } measuredWith = this.ctx.measureText(text).width; @@ -205,9 +209,14 @@ export default class Painter { // 如果是最后一行了,发现还有未绘制完的内容,则加... if (i === lines - 1 && start < view.text.length) { while (this.ctx.measureText(`${text}...`).width > width) { + if (text.length <= 1) { + // 如果只有一个字符时,直接跳出循环 + break; + } text = text.substring(0, text.length - 1); } text += '...'; + measuredWith = this.ctx.measureText(text).width; } const x = -(width / 2); const y = -(height / 2) + (i === 0 ? view.css.fontSize.toPx() : (view.css.fontSize.toPx() + i * lineHeight));