当前位置: 首页 > article >正文

鸿蒙5.0实战案例:基于RichEditor的评论编辑

往期推文全新看点(文中附带全新鸿蒙5.0全栈学习笔录)

✏️ 鸿蒙(HarmonyOS)北向开发知识点记录~

✏️ 鸿蒙(OpenHarmony)南向开发保姆级知识点汇总~

✏️ 鸿蒙应用开发与鸿蒙系统开发哪个更有前景?

✏️ 嵌入式开发适不适合做鸿蒙南向开发?看完这篇你就了解了~

✏️ 对于大前端开发来说,转鸿蒙开发究竟是福还是祸?

✏️ 鸿蒙岗位需求突增!移动端、PC端、IoT到底该怎么选?

✏️ 记录一场鸿蒙开发岗位面试经历~

✏️ 持续更新中……


场景描述

RichEditor是支持图文混排和文本交互式编辑的组件,在我们使用的APP中是很常见的,比如评论区编辑内容发布、对话框或者聊天室。下面列举一些遇到的场景需求:

  • 场景一:基于文字图片以及@信息的好友评论
  • 场景二:右下角的剩余字数
  • 场景三:评论中携带所@的用户的附属信息
  • 场景四:文本选择区域发生变化或编辑状态下光标位置发生变化回调
  • 场景五:自由切换系统键盘和自定义键盘。

方案描述

场景一:基于文字图片以及@信息的好友评论

效果图

方案

1、通过addTextSpan来添加文字,2、通过addImageSpan来添加图片3、通过addBuilderSpan来实现一段内容光标不可插入的效果4、addTextSpan等支持gesture等手势操作,比如onClick点击和onLongPress长按事件

核心代码

1、由于RichEditor是支持图文混排的,所以RichEditor里面的内容也就是由Text和Image组成的,分别通过addTextSpan和addImageSpan添加,而且还可以设置字体和图片样式,如下代码1:

@Entry

@Component

struct getSpans {

editorController = new RichEditorController()

build() {

Column() {

RichEditor({ controller: this.editorController })

.width('100%')

.backgroundColor(Color.Yellow)

.onReady(() => {

this.editorController.addImageSpan($r("app.media.icon"),

{

imageStyle:

{

size: ["100px", "100px"]

}

})

this.editorController.addTextSpan('男生女生向前冲',

{

style:

{

fontColor: Color.Blue,

fontSize: 30

}

})

})

Button('getSpans-图片').onClick((event: ClickEvent) => {

let getSpans = this.editorController.getSpans({ start: 0 })

console.log('getSpans0' + JSON.stringify(getSpans[0]))

// 必须进行强转才能获取文字信息或者图片信息

let span0 = getSpans[0] as RichEditorTextSpanResult

console.log('图片相关的信息: ' + JSON.stringify(span0))

})

Button('getSpans-文字').onClick((event: ClickEvent) => {

let getSpans = this.editorController.getSpans({ start: 0 })

console.log('getSpans1' + JSON.stringify(getSpans[1]))

let span1 = getSpans[1] as RichEditorImageSpanResult

console.log('文字相关的信息: ' + JSON.stringify(span1))

})

}

}

}

2、在常见的评论区艾特好友时,会将“@华为官方客服”视作一个整体,光标无法插入其中,且第一次按删除键时整体会高亮选中,第二次再按删除键才会删除。**需要注意的是,如果使用的全局@Builder,则需要配合bind(this)使用。**后续也可以通过事件阻止“即RichEditor控件ClickEvent/TouchEvent支持preventDefault”。

@Entry

@Component

struct BuilderSpan {

editorController = new RichEditorController()

@Builder

At(str: string) {

Stack() {

Text('@' + str).fontColor(Color.Blue)

}

}

build() {

Column() {

RichEditor({ controller: this.editorController })

.width('100%')

.height(50)

.backgroundColor(Color.Yellow)

.onReady(() => {

this.editorController.addBuilderSpan(() => {

this.At('华为官方客服')

})

this.editorController.addTextSpan('!!!')

})

}

}

}

3、发微博的时候可以输入表情、超链接、文字等一起的内容

代码3:

@Component

struct Index {

@State message: string = '#超话社区#';

controller: RichEditorController = new RichEditorController();

@State show: boolean = false;

build() {

Column() {

RichEditor({controller: this.controller})

.height(56)

.width("90%")

.borderStyle(BorderStyle.Solid)

.borderColor(Color.Black)

.borderWidth(1)

.onReady(() => {

})

Button('输入表情').onClick((event: ClickEvent) => {

this.controller.addImageSpan($r('app.media.app_icon'), {

imageStyle: {

size: ["80px", "80px"]

}

})

})

Button('超链接').onClick((event: ClickEvent) => {

this.controller.addTextSpan(this.message, {

offset: this.controller.getCaretOffset(),

style:

{

fontColor: Color.Orange,

fontSize: 16

},

gesture:

{

onClick: () => {

console.log('要跳转链接了哦')

},

onLongPress: () => {

console.log('我被长按了')

}

}

})

this.controller.setCaretOffset(this.controller.getCaretOffset() + this.message.length)

})

}

.width('100%')

}

}

场景二:右下角的剩余字数

效果图

方案

使用overlay浮层,显示“0/20”字数提示符在组件的右下角。再通过aboutToIMEInput回调(输入法输入内容前),获取插入的文本偏移位置,对输入内容进行限制。

核心代码

import promptAction from '@ohos.promptAction';

@Entry

@Component

struct MaxDemo {

@State message: string = '蜡笔小新';

controller: RichEditorController = new RichEditorController();

@State getContentLength: number = 0;

@State BOnSt:promptAction.ShowToastOptions = {'message': '已超出内容最大限制.'}

build() {

Column() {

RichEditor({ controller: this.controller })

.height(100)

.borderWidth(1)

.borderColor(Color.Red)

.width("100%")

.overlay(this.getContentLength + "/20", {

align: Alignment.BottomEnd

})

.aboutToIMEInput((value: RichEditorInsertValue) => {

console.log("insertOffset:" + JSON.stringify(value.insertValue)) // 插入的文本偏移位置。

if (this.getContentLength < 20) {

this.getContentLength = this.getContentLength + value.insertValue.length

console.log('实时的内容长度:' + this.getContentLength)

return true

}

promptAction.showToast(this.BOnSt)

return false

})

.aboutToDelete((value: RichEditorDeleteValue) => {

console.log('删除:' + JSON.stringify(value))

this.getContentLength = this.getContentLength - value.length

return true

})

}

.width('100%')

}

}

场景三:评论中携带所@的用户的附属信息

效果图

方案

使用HashMap的set向HashMap中添加或更新一组数据,使用get方法获取指定key所对应的value。

核心代码

@Entry

@Component

struct SameNameDemo {

controller: RichEditorController = new RichEditorController();

@Builder

At(str: string) {

Stack() {

Text('@' + str).fontColor(Color.Blue)

}

.onClick(() => {

// 添加不同的身份信息

const hashMap: HashMap<string, number> = new HashMap();

hashMap.set("friend1", 123);

let result = hashMap.get("friend1");

console.log('result: ' + result)

})

}

build() {

Column() {

RichEditor({ controller: this.controller })

.height(100)

.borderWidth(1)

.borderColor(Color.Red)

.width("100%")

Button('好友1').onClick((event: ClickEvent) => {

this.controller.addBuilderSpan(() => {

this.At('华为官方客服')

})

})

Button('好友2').onClick((event: ClickEvent) => {

this.controller.addBuilderSpan(() => {

this.At('华为官方客服')

})

})

}

.width('100%')

}

}

场景四:文本选择区域发生变化或编辑状态下光标位置发生变化回调

效果图

方案

使用RichEditor组件在API 12支持的新接口 onSelectionChange,在文本选择区域发生变化或编辑状态下光标位置发生变化时触发该回调。光标位置发生变化回调时,选择区域的起始位置等于终止位置(即start = end)。

核心代码

@Entry

@Component

struct Index {

@State message: string = '蜡笔小新';

controller: RichEditorController = new RichEditorController();

@State show: boolean = false;

build() {

Column() {

RichEditor({controller: this.controller})

.defaultFocus(true)

.padding(0)

.height(56)

.width("90%")

.borderStyle(BorderStyle.Solid)

.borderColor(Color.Black)

.borderWidth(1)

.padding({left: 10})

.onReady(() => {

this.controller.addTextSpan(this.message, {

offset: this.controller.getCaretOffset(),

style:

{

fontColor: Color.Orange,

fontSize: 16

}

})

this.controller.setCaretOffset(this.controller.getCaretOffset() + this.message.length)

})

.onSelectionChange((value:RichEditorRange) => {

console.log('光标位置改变了')

console.log('start: ' + value.start)

console.log('end: ' + value.end)

})

}

.width('100%')

}

}

场景五:自由切换系统键盘和自定义键盘

效果图

方案

使用RichEditor的属性customKeyboard控制切换系统键盘还是自定义键盘,添加表情使用addImageSpan,删除内容使用deleteSpans,并通过获取光标所在位置进行删除。

核心代码

@Entry

@Component

struct Index {

@State message: string = 'Hello World';

controller = new RichEditorController()

@State showKeyboard:boolean = false;

private listData: (string | number | Resource)[] = [

$r('app.media.img'), $r('app.media.img_1'), $r('app.media.img_2'), $r('app.media.img_3'), $r('app.media.img_4'), $r('app.media.img_5'), $r('app.media.img_6'),

$r('app.media.img'), $r('app.media.img_1'), $r('app.media.img_2'), $r('app.media.img_3'), $r('app.media.img_4'), $r('app.media.img_5'), $r('app.media.img_6'),

$r('app.media.img'), $r('app.media.img_1'), $r('app.media.img_2'), $r('app.media.img_3'), $r('app.media.img_4'), $r('app.media.img_5'), $r('app.media.img_6'),

$r('app.media.img'), $r('app.media.img_1'), $r('app.media.img_2'), $r('app.media.img_3'), $r('app.media.img_4'), $r('app.media.img_5'), $r('app.media.img_6'),

$r('app.media.img'), $r('app.media.img_1'), $r('app.media.img_2'), $r('app.media.img_3'), $r('app.media.img_4'), $r('app.media.img_5'), $r('app.media.img_6'),

];

// 自定义键盘组件

@Builder CustomKeyboardBuilder() {

Column() {

Text('自定义表情键盘')

.fontSize(25)

.fontWeight(900)

Grid() {

ForEach(this.listData, (item: string | number | Resource) => {

GridItem() {

if (typeof item !== 'number' && typeof item !== 'string') {

Image(item)

.width(30).onClick(() => {

this.controller.addImageSpan(item, { imageStyle: { size: ['110px', '110px'] } })

})

}

}

})

}.columnsGap(10).rowsGap(10).padding(5)

Row() {

Image($r('app.media.img_7'))

.width(30)

.onClick(() => {

this.controller.deleteSpans({start: this.controller.getCaretOffset() - 1, end: this.controller.getCaretOffset()})

})

}

.width('100%')

.justifyContent(FlexAlign.End)

.margin({ bottom: 40 })

}

.borderColor(Color.Gray)

.borderWidth(5)

}

build() {

Column() {

RichEditor({ controller: this.controller })

.width('100%')

.borderWidth(1)

.borderColor(Color.Black)

.onReady(() => {

this.controller.addTextSpan('有序排队')

})

.customKeyboard(this.showKeyboard ? this.CustomKeyboardBuilder() : undefined)

Button('切换系统键盘与自定义键盘').onClick((event: ClickEvent) => {

this.showKeyboard = ! this.showKeyboard

})

}

.height('100%')

}

}

http://www.kler.cn/a/562890.html

相关文章:

  • 特斯拉 FSD 算法深度剖析:软件层面全解读
  • 登录逻辑结合redis
  • 【Linux】vim 设置
  • Selenium 与 Coze 集成
  • 【AI+智造】基于DeepSeek的船舶海工设备多维度数据分析技术方案——以南通船舶制造企业为例
  • Element Plus: el-card的内容滚动问题
  • 钉钉MAKE AI生态大会思考
  • 一文讲解Redis中热点Key的重建、可能会发生的问题及解决方案
  • JVM 简单内存结构及例子
  • 前端(vue)学习笔记(CLASS 2):进阶
  • Rk3568驱动开发_点亮led灯(手动挡)_5
  • 认证与授权#1#Token和Cookie验证方式对比
  • 学习Java数组操作:从基础到高级技巧详解
  • 【算法工程】大模型局限性新发现之解决能连github但无法clone项目的问题
  • Java 设计模式 面试题及答案整理,最新面试题
  • 深度学习-133-LangGraph之应用实例(二)使用面向过程和面向对象的两种编程方式构建带记忆的聊天机器人
  • AcWing 蓝桥杯集训·每日一题2025·密接牛追踪2
  • 正则化技术
  • 【React】合成事件原理
  • 力扣hot100刷题——11~20