中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
RichEditor无法实现输入文字超过限制功能。
微信扫码分享
// 输入框文字校验 inputNumCheck(){ this.controllerRich.getSpans({ start: this.start, end: this.end}).forEach(item => { if (typeof (item as RichEditorImageSpanResult)['imageStyle'] !== 'undefined') { this.inputNum++ } else { this.inputNum = (item as RichEditorTextSpanResult).value.length; } }) } build() { Column() { Row(){ // 输入框 RichEditor({ controller: this.controllerRich}) .height($r('app.integer.chat_with_expression_chat_input_height')) .layoutWeight(LAYOUT_WEIGHT) .borderRadius($r('app.integer.chat_with_expression_chat_border_radius')) .backgroundColor($r('app.string.chat_with_expression_input_background')) .margin({ top: $r('app.integer.chat_with_expression_express_margin_top') ,left:14}) .key(this.focusKey) .id(this.focusKey) .placeholder('平台提倡文明用语,请温柔发言哦~',{font:{size:12},fontColor:'#999999'}) .defaultFocus(true) .aboutToIMEInput((value: RichEditorInsertValue)=>{ if (this.inputNum >= 50) { // 输入字符数超过50个 就禁止输入 try { promptAction.showToast({ message: '最多只能输入50个汉字哦', duration: 2000 }); } catch (error) { let message = (error as BusinessError).message let code = (error as BusinessError).code console.error(`showToast args error code is ${code}, message is ${message}`); }; return false } else { this.inputNumCheck() } return true })