往期推文全新看点(文中附带全新鸿蒙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%')
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |