HarmonyOS Text设置最大行高maxHeight以及textOverflow后文字依然超出边框显示?

代码如下:

Stack({alignContent:Alignment.TopStart}) {
  Image(this.model.background?.length?this.model.background:$r('app.media.update_app_dialog_bg'))
    .objectFit(ImageFit.Contain)
  Column() {
    // Text(this.model.content)
    Text('薪资打卡机打卡机打啊打卡机打卡机打卡啊打卡机打卡机打卡卡机打卡机可大可久打卡机打交道打卡机卡就打卡机打卡机打卡机的就打卡机打卡薪资打卡机打卡机打卡机打卡机可大可就打卡机打卡机打卡机的就打卡机打卡薪资打卡机打卡机打卡机打卡机可大可久打卡机打交道打卡机卡就打卡机打卡机打卡机的就打卡机打卡薪资打卡机打卡机打卡机打卡机可大可久打卡机打交道打卡机卡就打卡机打卡机打卡机的就打卡机打卡薪资打卡机打卡机打卡机打卡机可大可久打卡机打交道打卡机卡就机的就打卡机打卡薪资')
      .fontSize(14)
      .fontColor('#4e4852')
      .fontWeight(FontWeight.Regular)
      .margin({
        top:selfLpx(174),
        bottom:selfLpx(10),
        left:selfLpx(26),
        right:selfLpx(29)
      })
      .textOverflow({ overflow: TextOverflow.Ellipsis })
      .wordBreak(WordBreak.NORMAL)
      .backgroundColor(Color.Pink)
      .constraintSize({maxHeight: selfLpx(453-174-29-40.5-10)})

    Blank().layoutWeight(1)

    Row() {
      Image(this.model.ignoreButtonImg?.length?this.model.ignoreButtonImg:$r('app.media.update_app_btn_no'))
        .height(selfLpx(40.5))
        .width(selfLpx((375-37*2-19*2-8)/2))
        .margin({
          bottom:selfLpx(29),
          right:selfLpx(8)
        })
        .onClick(()=>{ // 取消
          this.controller.close()
        })

      Image(this.model.updateButtonImg?.length?this.model.updateButtonImg:$r('app.media.update_app_btn_now'))
        .height(selfLpx(40.5))
        .width(selfLpx((375-37*2-19*2-8)/2))
        .margin({
          bottom:selfLpx(29)
        })
        .onClick(()=>{ // 更新

        })

    }.justifyContent(FlexAlign.SpaceBetween)
    .width('100%')
    .padding({
      left:selfLpx(19),
      right:selfLpx(19)
    })
  }
}
.width(selfLpx(300))
.height(selfLpx(453))
阅读 445
1 个回答

可以参考

Text("主标题xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxsdgdsgfsdfgsdgsdfgsdfgsd")
  .maxLines(1)
  .textOverflow({overflow: TextOverflow.Ellipsis})

结合textOverflow属性来处理文本超长的情况。textOverflow属性可以设置文本超长时的显示方式,例如截断、省略号等。需要注意的是,textOverflow属性需要配合maxLines一起使用,单独设置不生效

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