【cocos creator】富文本设置,添加表情

// 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) {},
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烧仙草奶茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值