fix: 修复文字出框问题

This commit is contained in:
0JARVIS0 2019-12-03 18:14:58 +08:00
parent 4617b91743
commit a5e7c0040e

View File

@ -196,15 +196,15 @@ export default class Painter {
} }
const fontWeight = view.css.fontWeight === 'bold' ? 'bold' : 'normal'; const fontWeight = view.css.fontWeight === 'bold' ? 'bold' : 'normal';
const textStyle = view.css.textStyle === 'italic' ? 'italic' : '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.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; let lines = 0;
const linesArray = []; const linesArray = [];
for (let i = 0; i < textArray.length; ++i) { for (let i = 0; i < textArray.length; ++i) {
const textLength = this.ctx.measureText(textArray[i]).width; 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); const calLines = Math.ceil(textLength / partWidth);
// 取最长的作为 width // 取最长的作为 width
width = partWidth > width ? partWidth : width; width = partWidth > width ? partWidth : width;
@ -547,7 +547,7 @@ export default class Painter {
} }
let lineIndex = 0; let lineIndex = 0;
for (let j = 0; j < textArray.length; ++j) { 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 start = 0;
let alreadyCount = 0; let alreadyCount = 0;
for (let i = 0; i < linesArray[j]; ++i) { for (let i = 0; i < linesArray[j]; ++i) {