自定义label省略内容

继承label的自定义组件 当输入的内容过长是 自动省略自定义长度以后的内容

import ccclass = cc._decorator.ccclass;
import property = cc._decorator.property;

@ccclass()
class LongLabel extends cc.Label {

    @property(cc.Boolean)
    _useCutOff: boolean = true;

    @property({
        type: cc.Boolean
    })
    get useCutOff(): boolean {
        return this._useCutOff;
    }

    set useCutOff(value) {
        this._useCutOff = value;
        this.notify(this.realString);
    }

    @property(cc.Integer)
    _cutOffLength = 5;
    @property({
        type: cc.Integer
    })
    get cutOffLength(): number {
        return this._cutOffLength;
    }

    set cutOffLength(value) {
        this._cutOffLength = value;
        this.notify(this.realString);
    }


    @property({
        override: true,
    })
    _string: string | number = "";

    @property({
        override: true,
        multiline: true,
        tooltip: CC_DEV && 'i18n:COMPONENT.label.string',
        type: cc.String
    })
    get string(): string | number {
        return this._string;
    }

    set string(value) {
        this.realString = value;
        this.notify(value);
    }

    realString: string | number = "";


    notify(value) {
        if (this._sgNode) {
            let list = Array.from(value);
            if (this.useCutOff && list.length > this.cutOffLength) {
                this._string = list.slice(0, this.cutOffLength).join("") + "..."
            }
            else {
                this._string = value;
            }
            if (CC_EDITOR) {
                if (this.overflow === cc.Label.Overflow.SHRINK) {
                    this.fontSize = this._userDefinedFontSize;
                }
                this._debouncedUpdateSgNodeString();
            } else {
                this._updateSgNodeString();
            }
        }
    }

    onLoad() {
        if(this.realString){
            this.notify(this.realString);
        }
    }
}

export = LongLabel

string中输入数字 1234567 自动转换为12345…
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值