HarmonyOS 虚线下划线的实现方式

是否支持自定义下划线的样式,比如使用虚线的下划线

HarmonyOS
2024-12-26 16:00:01
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

虚线下划线可以用canvas自定义一个组件实现,demo如下:

@Entry
@Component
struct CanvasExample {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)

  /*
  * text: 绘制的文本内容
  * x: 开始位置的x坐标
  * y: 开始位置的y坐标
  * maxWidth: 最大换行宽度
  * lineHeight: 行高
  */
  wrapText(text: string, x: number, y: number, maxWidth?: number, lineHeight?: number) {
    if (typeof maxWidth == 'undefined') {
      maxWidth = 300;
    }
    if (typeof lineHeight == 'undefined') {
      lineHeight = 20
    }
    let arrText = text.split(''); // 字符分隔为数组
    let line = '';
    for (let n = 0; n < arrText.length; n++) {
      let testLine = line + arrText[n];
      let metrics = this.context.measureText(testLine);
      let testWidth = metrics.width;
      if (testWidth > maxWidth && n > 0) {
        this.context.fillText(line, x, y);
        this.context.beginPath();
        this.context.moveTo(x, y + 10);
        this.context.lineTo(maxWidth, y + 10);
        this.context.stroke();
        this.context.closePath();
        line = arrText[n];
        y += lineHeight;
      } else {
        line = testLine;
      }
    }
    this.context.fillText(line, x, y);
    this.context.beginPath();
    this.context.moveTo(x, y + 10);
    this.context.lineTo(maxWidth, y + 10);
    this.context.stroke();
    this.context.closePath();
  }
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context).width('100%').height('100%')
        .backgroundColor('#ffff00')
        .onReady(() => {
          this.context.lineWidth = 2 // 设置线条宽度
          this.context.strokeStyle = "red" // 设置线条颜色
          this.context.setLineDash([10, 3]) // 设置虚线间隔
          this.context.font = "45px"
          this.wrapText("我要换行我要换行,我要换行我要换行,我要换行我要换行,我要换行我要换行,我要换行我要换行,我要换行我要换行,我要换行我要换行,我要换行我要换行,我要换行我要换行,我要换行我要换行", 0, 40, this.context.width, 30)
        })
    }.width('100%').height('100%')
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-26 18:17:31


相关问题
如何给文字添加下划线?
1422浏览 • 1回复 待解决
tab-bar下方下划线怎么去掉?
4107浏览 • 1回复 待解决
希望提供字体是否为下划线文本接口
1331浏览 • 1回复 待解决
Tabs组件选中下划线需要平移动效
2273浏览 • 1回复 待解决
HarmonyOS 如何实现虚线
1514浏览 • 1回复 待解决
HarmonyOS 虚线功能实现
818浏览 • 1回复 待解决
实现一个虚线边框组件。
1480浏览 • 1回复 待解决
HarmonyOS HSP下载逻辑线下怎么调试
681浏览 • 1回复 待解决
给文本添加上划线如何实现
969浏览 • 1回复 待解决
Divider组件是否存在虚线属性
1637浏览 • 1回复 待解决
恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。