HarmonyOS 富文本组件RichEditor并不能编辑html格式数据,想实现html格式编辑的解决方案

需要富文本编辑组件,输入格式是html的数据,展示为相应的富文本,编辑富文本后拿到的还是html格式数据。当前困难影响:RichEditor的编辑功能很丰富,但是不能接收html格式数据,输出后的数据还是纯文本。

HarmonyOS
2024-12-26 15:01:08
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

目前还没有可以同时展示和编辑html的组件,可尝试下列方案,案例如下:

@Entry
@Component
struct RichTextExample {
  @State data: string = '<h1 style="text-align: center;">h1标题</h1>' +
    '<h1 style="text-align: center;"><i>h1斜体</i></h1>' +
    '<h1 style="text-align: center;"><u>h1下划线</u></h1>' +
    '<h2 style="text-align: center;">h2标题</h2>' +
    '<h3 style="text-align: center;">h3标题</h3>' ;
  controller: RichEditorController = new RichEditorController();
  options: RichEditorOptions = { controller: this.controller };
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start,
      justifyContent: FlexAlign.Start }) {
      RichText(this.data)
        .onStart(() => {
          console.info('RichText onStart');
        })
        .onComplete(() => {
          console.info('RichText onComplete');
        })
        .width('100%')
        .height(300)
        .backgroundColor(0XBDDB69)
      RichEditor(this.options)
        .onReady(()=>{
          this.controller.addTextSpan(this.data,
            {
              style:
              {
                fontColor: Color.Orange,
                fontSize: 30
              }
            })
        })
        .onIMEInputComplete((value: RichEditorTextSpanResult) => {
          this.data = value.value
        })
        .borderWidth(1)
        .borderColor(Color.Green)
        .width("100%")
        .height(400)
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
分享
微博
QQ
微信
回复
2024-12-26 16:46:29
相关问题
HarmonyOS Web加载HTML格式文本失败
689浏览 • 1回复 待解决
HarmonyOS html文本显示问题
2224浏览 • 1回复 待解决
HarmonyOS 文本组件问题
1305浏览 • 1回复 待解决
基于RichEditor评论编辑
1295浏览 • 1回复 待解决
HarmonyOS 如何支持html格式字符串?
653浏览 • 1回复 待解决
HarmonyOS @Entry 并不能封装到库使用 ?
572浏览 • 0回复 待解决
HarmonyOS @Entry 并不能封装到库使用
987浏览 • 1回复 待解决
如何关闭编辑器自动格式化?
1774浏览 • 1回复 待解决
HarmonyOS 部分文本高亮解决方案
1530浏览 • 1回复 待解决
HarmonyOS 计算文本宽高实现方案
942浏览 • 1回复 待解决
HarmonyOS 音频播放组件解决方案
992浏览 • 1回复 待解决
HarmonyOS 如何解析HTML文本
1390浏览 • 1回复 待解决
高级图表实现解决方案
1665浏览 • 1回复 待解决