继承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…