HarmonyOS 使用MutableStyledString setStyle报错?

使用MutableStyledString append文本 然后给指定的start - end之间插入特殊customspan 报错 Error: Input span type check failed.@122472bf

let styleString = new MutableStyledString("")
styleString.appendStyledString(new StyledString("第一个段落" + "\r\n"))
try {
  styleString.setStyle({
    styledKey: StyledStringKey.CUSTOM_SPAN,
    styledValue: new QDParagraphSpan(),
    start: start,
    length: end - start
  })
} catch (e) {
  readLogError(e)
}

export class QDParagraphSpan extends CustomSpan {
  onMeasure(measureInfo: CustomSpanMeasureInfo): CustomSpanMetrics {
    return { width: 0, height: 0 }
  }

  onDraw(context: DrawContext, drawInfo: CustomSpanDrawInfo): void {
  }
}
阅读 485
1 个回答

创建CustomSpan只接受 new MutableStyledString(new CustomSpan()) 如果需要使用setStyle来设置 只允许在本身就是CustomSpan上替换CustomSpan。

new MutableStyledString(new CustomSpan())的length只能是1。

考虑要在CustomSpan中存储数据,这样写看下:

export class QDParagraphSpan extends CustomSpan {
  x:number = 0;
  y:number = 0;
  type:string = "";
  data:string = "";

  constructor(inputX:number, inputY:number, inputType:string, inputData:string) {
    super();
    this.x = inputX;
    this.y = inputY;
    this.type = inputType;
    this.data = inputData;
  }
  onMeasure(measureInfo: CustomSpanMeasureInfo): CustomSpanMetrics {
    return { width: 0, height: 0 }
  }

  onDraw(context: DrawContext, drawInfo: CustomSpanDrawInfo): void {
  }
}

let styleString = new MutableStyledString("Hello World")
styleString.appendStyledString(
  new MutableStyledString(
    new QDParagraphSpan(100, 200, "qipao", "999+")
  )
)

customspan可以理解为一个文字字符占位,自绘制的,不能跨多个,长度都是1

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进