目录

实现占位文字跟随内容一起上滑

问题现象

设置一段占位文字,如何实现该占位文字无法被删除并且会随着输入数据的增加自动上滑的功能?

效果预览

背景知识

解决方案

  1. 使用Stack组件实现占位文字和文本输入框的组合。
  2. 在onContentScroll回调方法中,通过修改offsetY属性改变占位文字在Y轴上的相对偏移量,实现占位文字自动上滑的功能。
  3. 在onWillDelete回调方法中,通过deleteOffset的值判断当前光标是否可以向前缩进,以实现占位文字无法被删除的功能。

完整示例参考如下:

@Entry
@Component
struct StackExample {
  txt: string = '占位文本';
  @State text: string = ' ';
  @State textValue: string = '';
  @State txtWidth: number = 0;
  @State offsetY: number = 0;
  controller: TextAreaController = new TextAreaController();

  build() {
    Row() {
      Column() {
        // 使用Stack组件实现占位文字和文本输入框的组合。
        Stack({ alignContent: Alignment.TopStart }) {
          Column() {
            Text(this.txt)
              .margin({ left: 14 })
              .fontSize(15)
              .fontColor(Color.Blue)
              .onAreaChange((oldValue: Area, newValue: Area) => {
                this.txtWidth = newValue.width as number;
              })
              .offset({ x: 0, y: this.offsetY })
          }
          .width(300)
          .height(92)
          .alignItems(HorizontalAlign.Start)
          .border({ width: 1 })
          .margin({ top: 8 })
          .clip(true)

          TextArea({ text: this.text, controller: this.controller })
            .width(300)
            .height(100)
            .wordBreak(WordBreak.BREAK_ALL)
            .textIndent(this.txtWidth)
            .onChange((info) => {
              this.text = info;
              this.textValue = this.text.trim();
            })
              // 在onContentScroll回调方法中,通过修改offsetY属性改变占位文字在Y轴上的相对偏移量,实现占位文字自动上滑的功能。
            .onContentScroll((totalOffsetX: number, totalOffsetY: number) => {
              if ((this.getUIContext().px2vp(totalOffsetY) - 8) > 0) {
                this.offsetY = 0;
              } else if ((this.getUIContext().px2vp(totalOffsetY) - 8) < -16) {
                this.offsetY = -16;
              } else {
                this.offsetY = (this.getUIContext().px2vp(totalOffsetY) - 8);
              }
            })
              // 在onWillDelete回调方法中,通过deleteOffset的值判断当前光标是否可以向前缩进,以实现占位文字无法被删除的功能。
            .onWillDelete((info: DeleteValue) => {
              if (info.deleteOffset === 1 || this.text === ' ') {
                return false;
              }
              return true;
            })
            .onDidDelete((info: DeleteValue) => {
              if (info.deleteOffset === 0) {
                this.text = ' ';
              }
            })
            .onTextSelectionChange((selectionStart: number, selectionEnd: number) => {
              if (selectionStart === 0) {
                if (selectionStart === selectionEnd) {
                  this.controller.caretPosition(1);
                } else {
                  this.controller.setTextSelection(1, selectionEnd);
                }
              }
            })
        }
      }
      .width('100%')
    }
    .height('100%')
  }
}