HarmonyOS Text如何知道触发了TextOverflow.Ellipsis

HarmonyOS
14h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

可参考以下demo:

import measure from '@ohos.measure'
@Entry
@Component
struct MeasurePage {
  @State rawTitle: string = "1月16日,国务院新闻办公室举行新闻发布会,介绍2024年春运形势及工作安排。从2月9日(除夕)00:00到2月17日(正月初八)24:00,免费9天。1月16日,国务院新闻办公室举行新闻发布会,介绍2024年春运形势及工作安排"
  // @State rawTitle: string = "1月16日,国务院新闻办公室举行新闻发布会"
  @State title: string = this.rawTitle //
  @State suffixStr: string = ""
  expanded: Boolean = true
  titleWidth: number = 350
  needProcess: boolean = true
  ellipsis: string = "..."
  EXPAND_STR: string = "展开"
  COLLAPSE_STR: string = "收起"
  MAX_LINES: number =2;
  fontSize:number = 16

  aboutToAppear(): void {
    this.process();
  }
  process(): void {
    if (this.expanded) {
      this.collapseText();
    } else {
      this.expandText();
    }
  }
  expandText(): void {
    console.log('testTag: ' + this.needProcess);
    if (this.needProcess) {
      this.suffixStr = this.COLLAPSE_STR; //收起
      this.expanded = true;
      this.title = this.rawTitle;
    }
  }
  collapseText(): void {
    if (!this.needProcess) {
      return;
    }
    let titleSize : SizeOptions = measure.measureTextSize({
      textContent: this.rawTitle,
      constraintWidth: this.titleWidth,
      fontSize: this.fontSize
    })
    let twoLineSize : SizeOptions = measure.measureTextSize({
      textContent: this.rawTitle,
      constraintWidth: this.titleWidth,
      fontSize: this.fontSize,
      maxLines: this.MAX_LINES
    })
    //文本未超过限制行数,不进行展开、收起处理
    if ((titleSize.height as number) == (twoLineSize.height as number)) {
      this.needProcess = false;
      return;
    }
    console.log('test row height:' + titleSize.height)
    console.log('test twoLineSize height:' + twoLineSize.height)
    let clipTitle: string = this.rawTitle
    this.suffixStr = this.EXPAND_STR;
    while ((titleSize.height as number) > (twoLineSize.height as number)) {
      this.expanded = true;
      // 可以修改其他计算算法提高性能
      clipTitle = clipTitle.substring(0, clipTitle.length - 1);
      titleSize = measure.measureTextSize({
        // textContent: clipTitle + this.ellipsis + this.suffixStr,
        textContent: clipTitle + this.ellipsis ,
        constraintWidth: this.titleWidth,
        fontSize: this.fontSize
      })
      console.log("test while clipTitle:" + clipTitle)
      console.log("test while clipTitle height:" + titleSize.height)
    }
    console.log("test clipTitle:" + clipTitle)
    this.title = clipTitle + this.ellipsis
    this.expanded = false;
  }
  build() {
    Row() {
      Column() {
        Text(){
          Span(this.title)
        }
        .fontSize(this.fontSize)
        .id("title")
        .width(this.titleWidth)
        if (this.needProcess) {
          Text(this.suffixStr)
            .fontColor(Color.Orange)
            .onClick((event) => {
            this.process();
          })
        }
      }
      .width('100%')
      .alignItems(HorizontalAlign.Start)
    }
    .height('100%')

  }
}
分享
微博
QQ
微信
回复
12h前
相关问题
HarmonyOS Text.textOverflow超长时显示异常
574浏览 • 1回复 待解决
textOverflow怎么按字母截断
792浏览 • 1回复 待解决
有谁知道Menu菜单的触发方式
2187浏览 • 1回复 待解决
HarmonyOS text如何设置行间距
0浏览 • 0回复 待解决
HarmonyOS 如何设置全局Text默认颜色
381浏览 • 1回复 待解决
HarmonyOS Text如何显示 emjo 表情?
250浏览 • 1回复 待解决
HarmonyOS text组件如何实现部分圆角
11浏览 • 1回复 待解决
HarmonyOS如何测量Text组件的宽度呢
452浏览 • 1回复 待解决
长按事件如何重复触发
2087浏览 • 1回复 待解决