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) // 滚动条颜色 
    } 
  } 
}
  • 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.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
分享
微博
QQ
微信
回复
2024-07-22 19:48:18


相关问题
HarmonyOS RichEditor光标位置异常问题
943浏览 • 1回复 待解决
如何把一个Component画到Pixelmap上
2571浏览 • 1回复 待解决
如何把一个Component画到Pixelmap上呢?
3618浏览 • 1回复 待解决
HarmonyOS RichEditor内容高出能否滚动?
707浏览 • 1回复 待解决
HarmonyOS RichEditor 添加图片后光标消失
630浏览 • 1回复 待解决