群发朋友圈的网页title需要 限制64字节;
formatterTitle(bytes: number, title: string): string {
let len = 0;// 字节数
let str = '';// 处理的title
// 由于特殊字符以及表情会占两个字符;导致截取字符时会断开出现乱码,采用for of,或[...title]可解决;
for (let strItem of title) {
let pointNum = strItem.codePointAt(0) ?? 0;
len += (pointNum > 0xFFFF) ? 4 : (pointNum > 255 ? 3 : 1);
if (len <= bytes) {
str += strItem;
}
}
if (len > bytes) {
str += '...';
}
return str;
}