// Learn cc.Class:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
richText: cc.RichText,
editBox: cc.EditBox,
},
// LIFE-CYCLE CALLBACKS:
onLoad() {
this.index = 0;
this.EnjoyIndex = [];
},
start() {
},
hid() {
this.richText.node.active = false;
},
show() {
this.richText.node.active = true;
},
//添加字符
setText() {
//"ffadsfs /* 11/zdgdzgv /* 05/"
var str = this.editBox.string;
this.TransformationStr(str);
//this.show();
},
//添加表情
setEnjoy(number) {
if (this.editBox.string.length + 5 < this.editBox.maxLength) {
}
else {
return;
}
if (number < 10) {
this.editBox.string += "/*0" + number + "/";
} else {
this.editBox.string += "/*" + number + "/";
}
this.richText.string = this.editBox.string;
var str = this.editBox.string;
this.TransformationStr(str);
this.show();
},
//将字符转为表情
TransformationStr(str) {
this.richText.string = "";
this.editBox.string = "";
var arr = str.split("/*");
for (var i = 0; i < arr.length; i++) {
if (arr[i].slice(2, 3) === "/" && /^\d+$/.test(arr[i].slice(0, 2))) {
this.richText.string += "<img src='emoji_0" + arr[i].slice(0, 2) + "'/>";
this.richText.string += arr[i].slice(3);
this.editBox.string += "/*" + arr[i].slice(0, 2) + "/";
this.editBox.string += arr[i].slice(3);
} else {
this.richText.string += arr[i];
this.editBox.string += arr[i];
}
}
},
// update (dt) {},
});
【cocos creator】富文本设置,添加表情
最新推荐文章于 2021-10-03 18:32:29 发布