作为一个萌新,我只想说我是没有感情的粘贴工具!

let richText = this.viewNode.getChildByName('richText').getComponent(cc.RichText);
const str = "<u>hello</u><color=#ff0000>Red Text,</color><br/>"+
"<size=60>enlarge me,</size>"+
"<br/><outline color=red width=4>A label with <i>outline,</i></outline>"+
"<br/><b>This text will be rendered as bold,</b>"+
"<br/><i>This text will be rendered as italic。</i>";
this.richText(richText,str); private richText(richTextNode,str: string = "") {
const regex = /<.+?\/?>/g; // 匹配尖括号标签
const matchArr = str.match(regex);
const specialChar = "│";
const replaceStr = str.replace(regex, specialChar); // 标签数组
const textArr: string[] = replaceStr.split(specialChar); // 文字数组
const strArr: string[] = []; // 存放处理过的文字数组
let paraNum = 0; // 待替换参数个数
for (let text of textArr) {
// 非空字符替换成类似 $[0-n] 参数
if (text !== "") {
text = `$[${paraNum}]`;
paraNum += 1;
}
strArr.push(text);
}
let templetStr: string = strArr.join(specialChar); // 数组转成待替换字符串
for (let index = 0; index < textArr.length; index++) {
// 转换代替换字符串之后, 删除文字数组多余空字符
if (textArr[index] === "") {
textArr.splice(index, 1);
index = index - 1;
}
}
while (templetStr.search(specialChar) !== -1) {
// 数组转成的字符串原本 '特殊字符' 位置都是富文本标签的位置, 替换回标签
if (matchArr[0]) {
templetStr = templetStr.replace(specialChar, matchArr[0].toString());
matchArr.splice(0, 1);
} else {
templetStr = templetStr.replace(specialChar, "");// 空字符串替换,防止死循环
console.warn("matchArr not enough");
}
}
const lastStrArr: string[] = []; // 转换后富文本数组
const arrayParm: string[] = new Array(paraNum).fill(""); // 替换参数数组
for (let i = 0; i < textArr.length; i++) {
for (const text of textArr[i]) {
arrayParm[i] = arrayParm[i] + text;
let replaceStr1 = templetStr;
for (let index = 0; index < paraNum; index++) {
replaceStr1 = replaceStr1.replace(`$[${index}]`, arrayParm[index]);
}
lastStrArr.push(replaceStr1);
}
}
let lastStrIndex = 0;
const func = () => {
if (lastStrIndex >= lastStrArr.length) {
return;
}
richTextNode.string = lastStrArr[lastStrIndex];
lastStrIndex += 1;
setTimeout(() => {
func();
}, 100);
};
setTimeout(() => {
func();
}, 1000);
}

我只是战略性mark 打扰了~~

最新文章

  1. Visual Studio 不生成.vshost.exe和.pdb文件的方法
  2. 数据结构之hash表
  3. linux强大IDE——Geany配置说明
  4. JAVA与编译语言及解释语言的关系
  5. bootstrap注意事项(五)表单
  6. SQLServer2008-2012资源及性能监控—CPU使用率监控具体解释
  7. java设计模式综合项目实战视频教程
  8. (二叉树 BFS) leetcode513. Find Bottom Left Tree Value
  9. 使用JUnit进行类的测试(一)
  10. User模型扩展和自定义
  11. c#之函数
  12. Google&#39;s Machine Learning Crash Course #04# First Steps with TensorFlow
  13. c++中利用localtime_s函数格式化输出当地日期与时间
  14. Python 基础list 增删改查 解析
  15. 关于pycharm 打不开某些文件夹和文件打不开的问题
  16. Distributed1:链接服务器
  17. linux下升级npm以及node
  18. (2)-生成JSONObject的方法
  19. Spring Boot系列教程十:Spring boot集成Sentinel Redis
  20. Redis中RedisTemplate和Redisson管道的使用

热门文章

  1. splay树 1285 宠物收养所
  2. 题解 CF375D 【Tree and Queries】
  3. ajax案例_校验用户名
  4. vue-cli3.0的记录
  5. 实现Callable接口实现多线程
  6. mybatis-generator-gui如何打包成exe
  7. nrm切换npm包源
  8. es6语法中的arrow function=&gt;
  9. keras Model 3 共享的层
  10. 自定义string类