fix: 文字过长减除

This commit is contained in:
0JARVIS0 2019-12-19 12:15:39 +08:00
parent 81e6193b1f
commit 617d92f224

View File

@ -556,7 +556,8 @@ export default class Painter {
if (view.id) { if (view.id) {
let textWidth = 0; let textWidth = 0;
for (let i = 0; i < textArray.length; ++i) { for (let i = 0; i < textArray.length; ++i) {
textWidth = this.ctx.measureText(textArray[i]).width > textWidth ? this.ctx.measureText(textArray[i]).width : textWidth; const _w = this.ctx.measureText(textArray[i]).width
textWidth = _w > textWidth ? _w : textWidth;
} }
this.globalWidth[view.id] = width ? (textWidth < width ? textWidth : width) : textWidth; this.globalWidth[view.id] = width ? (textWidth < width ? textWidth : width) : textWidth;
if (!this.isMoving) { if (!this.isMoving) {
@ -570,6 +571,7 @@ export default class Painter {
const preLineLength = Math.ceil(textArray[j].length / linesArray[j]); const preLineLength = Math.ceil(textArray[j].length / linesArray[j]);
let start = 0; let start = 0;
let alreadyCount = 0; let alreadyCount = 0;
for (let i = 0; i < linesArray[j]; ++i) { for (let i = 0; i < linesArray[j]; ++i) {
// 绘制行数大于最大行数,则直接跳出循环 // 绘制行数大于最大行数,则直接跳出循环
if (lineIndex >= lines) { if (lineIndex >= lines) {
@ -580,11 +582,16 @@ export default class Painter {
let measuredWith = this.ctx.measureText(text).width; let measuredWith = this.ctx.measureText(text).width;
// 如果测量大小小于width一个字符的大小则进行补齐如果测量大小超出 width则进行减除 // 如果测量大小小于width一个字符的大小则进行补齐如果测量大小超出 width则进行减除
// 如果已经到文本末尾,也不要进行该循环 // 如果已经到文本末尾,也不要进行该循环
while ((start + alreadyCount <= textArray[j].length) && (width - measuredWith > view.css.fontSize.toPx() || measuredWith > width)) { while ((start + alreadyCount <= textArray[j].length) && (width - measuredWith > view.css.fontSize.toPx() || measuredWith - width > view.css.fontSize.toPx())) {
if (measuredWith < width) { if (measuredWith < width) {
text = textArray[j].substr(start, ++alreadyCount); text = textArray[j].substr(start, ++alreadyCount);
} else { } else {
break; if (text.length <= 1) {
// 如果只有一个字符时,直接跳出循环
break;
}
text = textArray[j].substr(start, --alreadyCount);
// break;
} }
measuredWith = this.ctx.measureText(text).width; measuredWith = this.ctx.measureText(text).width;
} }
@ -728,4 +735,4 @@ export default class Painter {
_getAngle(angle) { _getAngle(angle) {
return Number(angle) * Math.PI / 180; return Number(angle) * Math.PI / 180;
} }
} }