HarmonyOS鸿蒙Next中Text.maxLines(2) .textOverflow({ overflow: TextOverflow.Ellipsis })的使用

发布于 1周前 作者 itying888 来自 鸿蒙OS

HarmonyOS鸿蒙Next中Text.maxLines(2) .textOverflow({ overflow: TextOverflow.Ellipsis })的使用 【设备信息】Mate 60
【API版本】Api14
【DevEco Studio版本】5.0.7.200

判断文案是否…显示

Text设置了最大行数以及裁剪属性之后,有没有api能获取已经折叠
android 可以getText来获取屏幕显示的内容,
鸿蒙的要怎么获取呢

4 回复

可以参考这个展开收起的demo,使用文本计算去实现,可以自行更改实现你要的效果,参考文档: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-measure-V5#measuretextmeasuretext

import inspector from '@ohos.arkui.inspector';
import componentUtils from '@ohos.arkui.componentUtils';
import measure from '@ohos.measure'

@Entry
@Component
export struct TextButton {
  @State content: string = '#限定的校服,2222222222222222222222222222222222222223333'
  private measureText = this.content;
  @State maxLines: number = Infinity
  @State isCollapse: boolean = true; // true收起 false展开
  @State collapseTxt: string = '…[展开]';
  private subTxt: string = '';
  @State flag: boolean = false; // 是否需要展开收起

  build() {
    Row() {
      Column() {
        Text() {
          if(this.flag){
            Span(this.collapseTxt)
              .onClick((){
                if(this.isCollapse){
                  this.maxLines = Infinity;
                  this.content = this.measureText;
                  this.isCollapse = false;
                  this.collapseTxt = '[收起]';
                } else {
                  this.isCollapse = true;
                  this.collapseTxt = '…[展开]';
                  this.content = this.subTxt;
                }
              })
          }
        }
        .textOverflow({ overflow: TextOverflow.Ellipsis })
        .maxLines(this.maxLines)
        .fontSize(24)
        .width(300)
        .backgroundColor(Color.White)
        .key('txt_info')
      }
      .width('100%')
    }
    .height('100%')
  }
  listener:inspector.ComponentObserver = inspector.createComponentObserver('txt_info')

  aboutToAppear() {
    let onLayoutComplete:()=>void=():void=>{
      if(this.isCollapse){
        this.getComponentRect('txt_info');
      }
    }
    let onDrawComplete:()=>void=():void=>{
      if(this.isCollapse) {
        this.subTxt = this.content;
      }
    }
    let FuncLayout = onLayoutComplete
    let FuncDraw = onDrawComplete
    this.listener.on('layout', FuncLayout)
    this.listener.on('draw', FuncDraw)
  }
  private getComponentRect(key:string) {
    let modePosition = componentUtils.getRectangleById(key);
    let localOffsetHeight = modePosition.size.height;
    const textSize : SizeOptions = measure.measureTextSize({ textContent: this.content, fontSize: 24, constraintWidth: 300 })
    const textSize2 : SizeOptions = measure.measureTextSize({ textContent: this.content, fontSize: 24, maxLines: 3, constraintWidth: 300 })
    if((textSize.height || 0) > (textSize2.height || 0)) this.flag = true;
    console.info(`localOffsetHeight ${localOffsetHeight} textSize${textSize.height} textSize2 ${textSize2.height}`)
    if(localOffsetHeight > (textSize2.height || 300)){
      this.content = this.content.substring(0, this.content.length - 2);
      this.isCollapse = true;
    }
  }
}

更多关于HarmonyOS鸿蒙Next中Text.maxLines(2) .textOverflow({ overflow: TextOverflow.Ellipsis })的使用的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


鸿蒙是响应式编程,无法对标Android动态从文本控件,获取getText。但是你的文本控件设置Text.maxLines(2) .textOverflow({ overflow: TextOverflow.Ellipsis })之前,对应传入的文本内容数据是自己可控的,可以直接获取该文本变量。

代码示例:

@Entry
@Component
struct TextContentExample {
  private textValue: string = '这是一段文本,在实际应用中可能会有很长的内容,这里只是作为示例展示文本超出最大行数时如何显示省略号。';

  build() {
    Column({ space: 50 }) {
      Text(this.textValue)
        .maxLines(2) // 设置最大行数为 2
        .overflow(TextOverflow.Ellipsis) // 超出部分显示省略号
      Button('获取文本内容')
        .onClick(() => {
          console.log(`获取到的文本内容是: ${this.textValue}`);
        })
    }
    .width('100%')
  }
}

望采纳。

在HarmonyOS鸿蒙Next中,Text.maxLines(2).textOverflow({ overflow: TextOverflow.Ellipsis })的联合使用用于控制文本的显示方式。Text.maxLines(2)限制文本最多显示2行,超出部分将被截断。.textOverflow({ overflow: TextOverflow.Ellipsis })则指定当文本超出显示范围时,使用省略号(…)来表示被截断的文本。这种组合适用于需要在有限空间内显示多行文本,并在超出时优雅地处理截断的场景。

在HarmonyOS鸿蒙Next中,Text.maxLines(2)textOverflow({ overflow: TextOverflow.Ellipsis })结合使用,可以限制文本最多显示2行,并在超出时显示省略号(…)。具体代码如下:

Text('这是一段很长的文本内容,超出两行时将显示省略号...')
  .maxLines(2)
  .textOverflow({ overflow: TextOverflow.Ellipsis })

这段代码确保文本在超过两行时自动截断,并在末尾显示省略号,适用于需要控制文本显示长度的场景。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!