diff --git a/lib/pen.js b/lib/pen.js index a83f060..4bfed02 100644 --- a/lib/pen.js +++ b/lib/pen.js @@ -196,15 +196,15 @@ export default class Painter { } const fontWeight = view.css.fontWeight === 'bold' ? 'bold' : 'normal'; const textStyle = view.css.textStyle === 'italic' ? 'italic' : 'normal'; - view.css.fontSize = view.css.fontSize ? view.css.fontSize : '20rpx'; + const fontSize = view.css.fontSize ? view.css.fontSize.toPx() : '20rpx'.toPx(); this.ctx.font = `${textStyle} ${fontWeight} ${view.css.fontSize.toPx()}px ${view.css.fontFamily ? view.css.fontFamily : 'sans-serif'}`; - // this.ctx.setFontSize(view.css.fontSize.toPx()); // 计算行数 let lines = 0; const linesArray = []; for (let i = 0; i < textArray.length; ++i) { const textLength = this.ctx.measureText(textArray[i]).width; - const partWidth = view.css.width ? view.css.width.toPx(false, this.style.width) - paddings[1] - paddings[3] : textLength; + let partWidth = view.css.width ? view.css.width.toPx(false, this.style.width) - paddings[1] - paddings[3] : textLength; + partWidth = partWidth > fontSize ? partWidth - partWidth % fontSize + fontSize : fontSize const calLines = Math.ceil(textLength / partWidth); // 取最长的作为 width width = partWidth > width ? partWidth : width; @@ -547,7 +547,7 @@ export default class Painter { } let lineIndex = 0; for (let j = 0; j < textArray.length; ++j) { - const preLineLength = Math.ceil(textArray[j].length / linesArray[j]); + const preLineLength = Math.floor(textArray[j].length / linesArray[j]); let start = 0; let alreadyCount = 0; for (let i = 0; i < linesArray[j]; ++i) {