const context = wx.createCanvasContext( 'myCanvas' )
var text = '这是一段文字用于文本自动换行文本长度自行设置欢迎大家指出缺陷' ;
var chr =text.split( "" );
var temp = "" ;
var row = [];
context.setFontSize(18)
context.setFillStyle( "#000" )
for ( var a = 0; a < chr.length; a++) {
if (context.measureText(temp).width < 250) {
temp += chr[a];
}
else {
a--;
row.push(temp);
temp = "" ;
}
}
row.push(temp);
if (row.length > 2) {
var rowCut = row.slice(0, 2);
var rowPart = rowCut[1];
var test = "" ;
var empty = [];
for ( var a = 0; a < rowPart.length; a++) {
if (context.measureText(test).width < 220) {
test += rowPart[a];
}
else {
break ;
}
}
empty.push(test);
var group = empty[0] + "..."
rowCut.splice(1, 1, group);
row = rowCut;
}
for ( var b = 0; b < row.length; b++) {
context.fillText(row[b], 10, 30 + b * 30, 300);
}
context.draw()
|
把string转成数组是多余的
确实有点多余