RichEditor上面有个Component,现在希望RichEditor和Component成为一个整体,能够自适应光标位置进行滚动,

RichEditor上面有个Component,现在希望RichEditor和Component成为一个整体,能够自适应光标位置进行滚动,应该如何实现,目前用Scroller无法获取RichEditor中光标的具体坐标(光标基于RichEditor的x和y位置),无法实现滚动。

HarmonyOS
2024-07-22 12:01:14
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

用Scroll将RichEditor和Component包裹,通过onAreaChange的回调,调用scroller.scrollBy的能力,以此来改变滚动组件的高度。

参考代码:

@Entry 
@Component 
struct Index { 
  editorController = new RichEditorController() 
  scroller: Scroller = new Scroller() 
 
  build() { 
    Column() { 
      Scroll(this.scroller) { 
        Column() { 
          Image($r('app.media.startIcon')) 
            .width('100%') 
            .height(200) 
            .margin({ bottom: 20 }) 
          RichEditor({ controller: this.editorController }) 
            .id('RichEditor') 
            .width('100%') 
            .backgroundColor(Color.Yellow) 
            .onReady(() => { 
              this.editorController.addImageSpan($r("app.media.startIcon"), 
                { 
                  imageStyle: 
                  { 
                    size: ["100px", "100px"] 
                  } 
                }) 
              this.editorController.addTextSpan('男生女生向前冲', 
                { 
                  style: 
                  { 
                    fontColor: Color.Blue, 
                    fontSize: 30 
                  } 
                }) 
            }) 
            .onAreaChange((_, value) => { 
              if (_.height !== value.height) { 
                this.scroller.scrollBy(0, Number(value.height) - 200) 
                console.log('---_.height', _.height) 
                console.log('---value.height', value.height) 
              } 
            }) 
          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)) 
          }) 
          Button('RichEditor获焦').onClick(() => { 
            focusControl.requestFocus('RichEditor') 
          }) 
        } 
      } 
      .scrollable(ScrollDirection.Vertical) // 滚动方向纵向 
      .scrollBar(BarState.On) // 滚动条常驻显示 
      .scrollBarColor(Color.Gray) // 滚动条颜色 
    } 
  } 
}
分享
微博
QQ
微信
回复
2024-07-22 19:48:18
相关问题
如何把一个Component画到Pixelmap上
1728浏览 • 1回复 待解决
如何把一个Component画到Pixelmap上呢?
2381浏览 • 1回复 待解决
HarmonyOS RichEditor内容高出能否滚动?
63浏览 • 1回复 待解决
自适应页面滚动如何实现
257浏览 • 1回复 待解决
A是Component, B是Dialog, C是Component
82浏览 • 1回复 待解决
RichEditor如何拿到BuilderSpan?
1530浏览 • 1回复 待解决