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

news/2025/2/27 9:13:18

往期推文全新看点(文中附带全新鸿蒙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.niftyadmin.cn/n/5869856.html

相关文章

C++ 二叉搜索树与双向链表_牛客题霸_牛客网

点击链接即可查看题目: 二叉搜索树与双向链表_牛客题霸_牛客网 一、题目 描述 输入一棵二叉搜索树&#xff0c;将该二叉搜索树转换成一个排序的双向链表。如下图所示 数据范围&#xff1a;输入二叉树的节点数 0≤n≤10000≤n≤1000&#xff0c;二叉树中每个节点的值 0≤val≤10…

Go中slice和map引用传递误区

背景 关于slice和map是指传递还是引用传递&#xff0c;很多文章都分析得模棱两可&#xff0c;其实在Go中只有值传递&#xff0c;但是很多情况下是因为分不清slice和map的底层实现&#xff0c;所以导致很多人在这一块产生疑惑&#xff0c;下面通过代码案例分析slice和map到底是…

【Linux】调试工具GDB的使用及案例讲解

Linux系列 文章目录 Linux系列前言一、gdb的使用背景二、gdb的使用总结 本篇主要针对小白讲解&#xff0c;可以很多地方比较咯嗦 前言 GDB是Linux下一款强大的调试工具。GDB可以调试C、C、Java等语言&#xff0c;对于在Linux下工作的程序员来说&#xff0c;GDB是必不可少的调试…

数据存储:一文掌握存储数据到MongoDB详解

文章目录 一、环境准备1.1 安装MongoDB1.2 安装Python MongoDB驱动 二、连接到MongoDB2.1 基本连接2.2 连接到MongoDB Atlas&#xff08;云服务&#xff09; 三、基本CRUD操作3.1 创建&#xff08;Create&#xff09;&#xff1a;插入数据3.2 读取&#xff08;Read&#xff09;…

【学习方法】学习软件专业课程的思考方式

学习软件专业课程的思考方式 在学习软件专业课程时&#xff0c;我们往往会遇到一些看似简单但实际上却非常复杂的概念和理论。这种时候&#xff0c;我们可能会觉得书本很厚&#xff0c;难以理解。然而&#xff0c;这种看似简单的想法并不一定就是错误的&#xff0c;因为它激发…

去中心化技术P2P框架

中心化网络与去中心化网络 1. 中心化网络 在传统的中心化网络中&#xff0c;所有客户端都通过一个中心服务器进行通信。这种网络拓扑结构通常是一个星型结构&#xff0c;其中服务器作为中心节点&#xff0c;每个客户端只能与服务器通信。如果客户端之间需要通信&#xff0c;必须…

ElasticSearch 是如何实现分布式的?

ElasticSearch 是如何实现分布式的&#xff1f; 如果你正在准备技术面试&#xff0c;或者想深入理解 ElasticSearch&#xff08;简称 ES&#xff09;是如何实现分布式的&#xff0c;这篇文章将用通俗易懂的方式来讲解。 1. 为什么需要分布式搜索&#xff1f; 在互联网行业&a…

【Python爬虫(80)】当Python爬虫邂逅边缘计算:探索数据采集新境界

【Python爬虫】专栏简介&#xff1a;本专栏是 Python 爬虫领域的集大成之作&#xff0c;共 100 章节。从 Python 基础语法、爬虫入门知识讲起&#xff0c;深入探讨反爬虫、多线程、分布式等进阶技术。以大量实例为支撑&#xff0c;覆盖网页、图片、音频等各类数据爬取&#xff…