span展开/收起,如何实现文本的展开收起的功能

新闻内容需要预布局,之后确认截断位置,加上…全文的span(这个span是有点击事件的)点击后可以展开全文

1. 话题不是仅在首位可能出现,出现次数也不为1。

2. 例如限制最大行数为3,希望看到如下图所示的效果,使文字恰好截断在第三行的位置,并且增加“…展开”的折叠span

3. 当点击折叠span后,可以展示全部文案。

span展开/收起,如何实现文本的展开收起的功能-鸿蒙开发者社区

HarmonyOS
2024-06-04 23:47:24
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
davis_li

可以使用接口import measure from '@ohos.measure'测算文字长度.

// 测量文本宽度(单位px) 
@State textWidth: number = measure.measureText({ 
  textContent: this.longMessage, 
  fontSize: 15 
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

示例代码:

import measure from '@ohos.measure' 
import curves from '@ohos.curves'; 
import { BusinessError } from '@ohos.base'; 
import display from '@ohos.display'; 
 
@Entry 
@Component 
struct Index { 
  build() { 
    Column() { 
      MyText() 
    } 
  } 
} 
 
@Component 
export struct MyText { 
  // 长文本 
  @State longMessage: string = "  走在繁华的城市街头,明空感到无比紧张。他的心跳如雷鼓般擂动着胸膛,使得身上的伪装仿佛随时都要被揭开。然而,他仍然保持着冷静,凭借着过人的胆识与智慧,成功地躲过了敌人的层层封锁。\n" + 
    "\n" + 
    "  最终,明空来到了敌对帮派的老巢。此时此刻,那里的守卫正沉浸在欢庆的氛围中,丝毫没有察觉到即将来临的危机。明空深吸一口气,压抑住内心的激动,悄然潜入了这座古老的建筑。\n" 
  // 最大显示行数 
  @State lines: number = 3; 
  // 长文本状态(展开 or 收起) 
  @State collapseText: string = '…展开全文' 
  // 屏幕宽度(单位px) 
  screenWidth: number = 0; 
  // 是否需要显示"展开"字样(注:当文本长度较短时就不需要"展开") 
  @State isExpanded: boolean = false 
  // 测量文本宽度(单位px) 
  @State textWidth: number = measure.measureText({ 
    textContent: this.longMessage, 
    fontSize: 15 
  }) 
  // 获取当前所有的display对象 
  promise: Promise<Array<display.Display>> = display.getAllDisplays() 
 
  aboutToAppear() { 
    console.log(`文本宽度为:${this.textWidth}`) 
    this.promise.then((data: Array<display.Display>) => { 
      console.log(`所有的屏幕信息:${JSON.stringify(data)}`) 
      //单位为像素 
      this.screenWidth = data[0]["width"] 
      // 屏幕宽度 * 最大行数 * 组件宽度比例 和 文字测量宽度 
      this.isExpanded = this.screenWidth * this.lines * 0.8 <= this.textWidth 
    }).catch((err: BusinessError) => { 
      console.error(`Failed to obtain all the display objects. Code: ${JSON.stringify(err)}`) 
    }) 
  } 
 
  build() { 
    Row() { 
      Column() { 
        if (this.isExpanded) { 
          Stack({ alignContent: Alignment.BottomEnd }) { 
            Text(this.longMessage) 
              .fontSize(15) 
              .fontColor(Color.Black) 
              .maxLines(this.lines) 
              .width('93%') 
            Row() { 
              Text(this.collapseText) 
                .fontSize(15) 
                .backgroundColor(Color.White) 
            } 
            .justifyContent(FlexAlign.End) 
            .onClick(() => { 
              if (this.collapseText == '…展开全文') { 
                this.collapseText = '…收起'; 
                // 展开动画 
                animateTo({ 
                  duration: 150, 
                  curve: curves.springMotion(0.5, 0.8), 
                }, () => { 
                  this.lines = -1; // 使得设置的最大行属性无效 
                }) 
              } else { 
                this.collapseText = '…展开全文'; 
                // 收起动画 
                animateTo({ 
                  duration: 100, 
                  curve: Curve.Friction, 
                }, () => { 
                  this.lines = 3; // 只显示3行 
                }) 
              } 
            }) 
          } 
        } 
        else { 
          Text(this.longMessage) 
            .fontSize(15) 
            .fontColor(Color.Black) 
        } 
      } 
      .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.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
分享
微博
QQ
微信
回复
2024-06-05 20:08:35
相关问题
如何实现文本展开收起功能
1409浏览 • 1回复 待解决
ArkTS实现Text文本【...展开
2537浏览 • 3回复 待解决
HarmonyOS 如何实现展开listview功能
535浏览 • 1回复 待解决
多行文本省略展开与显示
1882浏览 • 1回复 待解决
HarmonyOS 如何实现半屏展开
1012浏览 • 1回复 待解决
HarmonyOS如何收起键盘?
838浏览 • 1回复 待解决