HarmonyOS 如何实现长文本实现展开收起功能

HarmonyOS
2024-12-25 13:27:17
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

参考示例如下:

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
@Preview
export struct MyText {
  // 长文本
  @State longMessage: string =
    "ArkTS是HarmonyOS生态的应用开发语言。它在保持TypeScript(简称TS)基本语法风格的基础上,进一步通过规范强化静态检查和分析,使得在程序运行之前的开发期能检测更多错误,提升代码健壮性,并实现更好的运行性能。\n" +
      "\n" +
      "同时,提供了声明式UI范式、状态管理支持等相应的能力,让开发者可以以更简洁、更自然的方式开发高性能应用。\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('80%')
            Row() {
              Text(this.collapseText)
                .fontSize(15)
                .fontColor(Color.Green)
                .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.
  • 101.
  • 102.
分享
微博
QQ
微信
回复
2024-12-25 16:15:05
相关问题
如何实现文本展开收起功能
1404浏览 • 1回复 待解决
ArkTS实现Text文本的【...展开
2534浏览 • 3回复 待解决
HarmonyOS 如何实现展开的listview功能
535浏览 • 1回复 待解决
HarmonyOS 关于实现TextView富文本功能
860浏览 • 1回复 待解决
HarmonyOS 如何实现半屏展开
1007浏览 • 1回复 待解决
HarmonyOS 单行超长文本换行不生效
619浏览 • 1回复 待解决
webviewController的loadData无法显示长文本
2490浏览 • 1回复 待解决