计算一行大概放置多少字符串实例相关代码:
data(){
return{
zh: new RegExp("[\u4E00-\u9FA5]+"),
en: new RegExp("[A-Za-z]+"),
}
}
formatterString(name) {
let _width = 0;
let arr = [110, 170, 170];//每行的总宽
let strings = [];
let sub_i = 0; //位置
arr.map((total, index) => {
if (name.length == sub_i + 1) return;
for (let i in name) {
i = Number(i);
if (_width >= total) {
_width = 0;
name = name.substring(sub_i + 1);
return false;
} else {
let obj = this.sum(_width, name[i]);
_width = obj._width;
strings[index] = (strings[index] ? strings[index] : "") + name[i];
sub_i = i;
}
}
});
return strings;
},
sum(_width, str) {
if (this.en.test(str)) {
if (group1.includes(str)) {
_width = _width + 10;
} else if (group2.includes(str)) {
_width = _width + 5;
} else if (group3.includes(str)) {
_width = _width + 3;
} else {
_width = _width + 7;
}
} else if (this.zh.test(str)) {
_width = _width + 10;
} else {
//特殊字符
if (group4.includes(str)) {
_width = _width + 10;
} else if (group5.includes(str)) {
_width = _width + 5;
} else if (group6.includes(str)) {
_width = _width + 3;
} else {
_width = _width + 10;
}
}
return { _width: _width, str: str };
},