HarmonyOS Text超出父组件?

父布局宽高固定,内部有Text,文字内容过多,超过了父布局,怎么限制Text

阅读 537
1 个回答

请参考以下示例:

// xxx.ets
import { MeasureText } from '@kit.ArkUI';

@Entry
@Component
struct WebComponent {

  @State newsINfo:string  = '测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试出生时是菜市场是市场上'
  boxHeight:number = 80
  textSize : SizeOptions = MeasureText.measureTextSize({
    textContent: this.newsINfo,
    fontSize: '15px'
  })
  build() {
    Column() {

      Row(){
        Text(this.newsINfo)
          .backgroundColor(Color.Green)
          .textOverflow({overflow:TextOverflow.Clip})
          .width('100%')
          .height('100%')
          .fontSize(15)
          .maxLines(Math.floor(this.boxHeight/Number(this.textSize.height)))
      }
      .width(150)
      .height(this.boxHeight)
      .backgroundColor(Color.Brown)
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进