用户3096723
用户3096723
  • 发布:2026-07-07 11:43
  • 更新:2026-07-07 11:43
  • 阅读:27

Text组件字符超过特定长度时,超出部分如何隐藏与显示

分类:uni-app

问题现象
在Text中显示文本时,如果文本超过一定长度,通常会有超出部分隐藏与显示的需求,例如:当最多显示的行数为2,组件宽度比例为0.4时,该如何实现?

背景知识
measureText方法能够根据文本信息计算文本宽度。
getAllDisplays方法能够获取display对象,display对象的width属性为屏幕的宽度。
点击放大

解决方案
首先设定的文本长度计算方式:设定文本长度 = 屏幕宽度 最大行数 组件宽度比例。屏幕宽度可以使用getAllDisplays获取。
然后使用measureText方法测量实际文本宽度,比较“设定文本长度”与“实际文本宽度”进行大小比较,判断是否需要隐藏。
当需要隐藏时,只展示“设定长度”的文本内容,超出部分显示为“...”。当点击“…”时将该文本变为“…收起”,显示隐藏部分内容。
完整示例参考如下:

import { MeasureUtils } from '@kit.ArkUI';
import curves from '@ohos.curves';
import { BusinessError } from '@ohos.base';
import display from '@ohos.display';

@Entry
@Component
struct Index {
// 长文本
longMessage: string = '走在繁华的城市街头,明空感到无比紧张。他的心跳如雷鼓般擂动着胸膛,使得身上的伪装仿佛随时都要被揭开。然而,他仍然保持着冷静,凭借着过人的胆识与智慧,成功地躲过了敌人的层层封锁。\n' +
'\n' +
'  最终,明空来到了敌对帮派的老巢。此时此刻,那里的守卫正沉浸在欢庆的氛围中,丝毫没有察觉到即将来临的危机。明空深吸一口气,压抑住内心的激动,悄然潜入了这座古老的建筑。';
// 最大显示行数
@State lines: number = 2;
// 长文本状态(展开 or 收起)
@State collapseText: string = '...';
// 屏幕宽度(单位px)
screenWidth: number = 0;
// 是否需要显示"展开"字样(注:当文本长度较短时就不需要“展开”)
@State isExpanded: boolean = false;
uiContext: UIContext = this.getUIContext();
uiContextMeasure: MeasureUtils = this.uiContext.getMeasureUtils();
// 测量文本宽度(单位px)
textWidth: number = this.uiContextMeasure.measureText({
textContent: this.longMessage,
fontSize: 20
});
// 获取当前所有的display对象
promise: Promise<Array<display.Display>> = display.getAllDisplays();

aboutToAppear() {
console.info(文本宽度为:${this.textWidth});
this.promise.then((data: Array<display.Display>) => {
console.info(所有的屏幕信息:${JSON.stringify(data)});
// 单位为像素
this.screenWidth = data[0]["width"];
// 屏幕宽度 最大行数 组件宽度比例 和 文字测量宽度
this.isExpanded = this.screenWidth this.lines 0.4 <= 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(20)
.fontColor(Color.Black)
.maxLines(this.lines)
.width("40%")
Row() {
Text(this.collapseText)
.fontSize(20)
.backgroundColor(Color.White)
}
.justifyContent(FlexAlign.End)
.onClick(() => {
if (this.collapseText == '...') {
this.collapseText = '...收起';
// 展开动画
this.uiContext.animateTo({
duration: 150,
curve: curves.springMotion(0.5, 0.8),
}, () => {
this.lines = -1; // 使得设置的最大行属性无效
});
} else {
this.collapseText = '...';
// 收起动画
this.uiContext.animateTo(
{
duration: 100,
curve: Curve.Friction,
}, () => {
this.lines = 2; // 只显示2行
});
}
})
}
}
else {
Text(this.longMessage)
.fontSize(20)
.fontColor(Color.Black)
}
}
.width('100%')
}
.height('100%')
}
}
https://pastebin.com/yYjzfBYh
https://pastebin.com/KvWVh2mC
https://pastebin.com/1e4eFFEG
https://pastebin.com/P3QEZ0Vt
https://pastebin.com/ySGX6wUJ
https://pastebin.com/jFJ9dwM8
https://pastebin.com/F8rBVMEp
https://pastebin.com/Th3G7tkh
https://pastebin.com/SN1eWG5Z
https://pastebin.com/eMtbaeZm
https://pastebin.com/7G7Ad9d5
https://pastebin.com/wAGuhNej
https://pastebin.com/Cmfya5mB
https://pastebin.com/pjPAkDdh
https://pastebin.com/C4124mFd
https://pastebin.com/1wHR87LD
https://pastebin.com/pccVzdhD
https://pastebin.com/J5MsUs86
https://pastebin.com/3DcWSqk8
https://pastebin.com/jFg0ThQC
https://pastebin.com/5xAUfgws
https://pastebin.com/6vaGRrFj
https://pastebin.com/jzHNkpVz
https://pastebin.com/6qZYe5TF
https://pastebin.com/QhC6D1Ea
https://pastebin.com/RHDyMM5i
https://pastebin.com/u6YdPYNT
https://pastebin.com/vMfTX3f8
https://pastebin.com/JfQ5h4mh
https://pastebin.com/eZTzxZXf
https://pastebin.com/ebALnXDi
https://pastebin.com/KgWVfrxD
https://pastebin.com/qN9C02WR
https://pastebin.com/wfmDCen7
https://pastebin.com/TRCTJxzH
https://pastebin.com/zUwZttGi
https://pastebin.com/tP3krksd
https://pastebin.com/a4zMuKAf
https://pastebin.com/Qd26iPzX
https://pastebin.com/QsYfKNGq
https://pastebin.com/ZBRQrP7g
https://pastebin.com/eLtxHFyq
https://pastebin.com/FFH804TB
https://pastebin.com/mpwpwtk1
https://pastebin.com/9MU20Mx1
https://pastebin.com/P8t51jjE
https://pastebin.com/yggX6dVr
https://pastebin.com/73KZ5Ewf
https://pastebin.com/B4s8zydV
https://pastebin.com/KvXBdDAh
https://pastebin.com/jfSidFKG
https://pastebin.com/i6z3pEGD
https://pastebin.com/K4wDCGB4
https://pastebin.com/Fk5vZKih
https://pastebin.com/j47NvPir
https://pastebin.com/r7jvjYg6
https://pastebin.com/BJzmiF4Q
https://pastebin.com/T983TDL4
https://pastebin.com/w03GsEzJ
https://pastebin.com/eVJ6DFrh
https://pastebin.com/JT5m4Fcd
https://pastebin.com/2Lyw6s6U
https://pastebin.com/nNnz4WSG
https://pastebin.com/zYw0vSEs
https://pastebin.com/7GNKsiA4
https://pastebin.com/1G8Ns8jL
https://pastebin.com/PfzWDnKs
https://pastebin.com/abuDWb37
https://pastebin.com/cV910k6U
https://pastebin.com/d1mmsxZD
https://pastebin.com/G9FgAyi3
https://pastebin.com/bAeBEub8
https://pastebin.com/1khz4e3X
https://pastebin.com/vxgkb5LD
https://pastebin.com/agcLMv2d
https://pastebin.com/nbSGyLLE
https://pastebin.com/fBXRb652
https://pastebin.com/AEaYhdxJ
https://pastebin.com/QnynP0t9
https://pastebin.com/KKG6w0k7
https://pastebin.com/s0iiVSxp
https://pastebin.com/EcmwFtbt
https://pastebin.com/KNmHDurJ
https://pastebin.com/CH32Naac
https://pastebin.com/DX2L75yh
https://pastebin.com/fkvZzYWa
https://pastebin.com/YH5AVbn3
https://pastebin.com/nkY9vSR2
https://pastebin.com/qQ9CkVRM
https://pastebin.com/cYMgDT9Q
https://pastebin.com/NCtDqJeU
https://pastebin.com/umbABgNV
https://pastebin.com/Jb0RrAxC
https://pastebin.com/nWHXx7p0
https://pastebin.com/Atbf7RdX
https://pastebin.com/yDd2FNsG
https://pastebin.com/NEfD7g3Z
https://pastebin.com/rZ1AkzAZ
https://pastebin.com/3HCfn9P1
https://pastebin.com/GWrtDxQr
https://pastebin.com/sf7fhcWs
https://pastebin.com/K7UL8AFj
https://pastebin.com/w4Hm6FuE
https://pastebin.com/5f0KTubZ
https://pastebin.com/6X5dsStC
https://pastebin.com/3nP0pK0t
https://pastebin.com/Emmq0RgR
https://pastebin.com/An3zQJse
https://pastebin.com/6h38FzmU
https://pastebin.com/y3GcsSec
https://pastebin.com/xnNtJTAg
https://pastebin.com/dZAGmXFz
https://pastebin.com/FZScNNPE
https://pastebin.com/8nLrkahp
https://pastebin.com/qx0Nznfz
https://pastebin.com/bbj21hbF
https://pastebin.com/QwZEg5QW
https://pastebin.com/A6Ph6wSg
https://pastebin.com/v8YbNB2J
https://pastebin.com/1Me85UF8
https://pastebin.com/N5zPTGhM
https://pastebin.com/cxR3Z0Q9
https://pastebin.com/yPptx6L2
https://pastebin.com/MgwpSDkL
https://pastebin.com/PdQkppe3
https://pastebin.com/ybfJNJTG
https://pastebin.com/4Cxexqf9
https://pastebin.com/cAShGPFD
https://pastebin.com/7iKLHyRr
https://pastebin.com/mttz0KMU
https://pastebin.com/EVrThu9E
https://pastebin.com/Y50asBDv
https://pastebin.com/95e2yHKf
https://pastebin.com/75zBX7Lg
https://pastebin.com/H88viQHj
https://pastebin.com/nJai6ZvU
https://pastebin.com/XGQr0J9c
https://pastebin.com/B26473RK
https://pastebin.com/ZkMRJwsT
https://pastebin.com/A7eaF21y
https://pastebin.com/qJrNz3u4
https://pastebin.com/2Egf1rmF
https://pastebin.com/ZDSc4Det
https://pastebin.com/DEGVVTgx
https://pastebin.com/MgRWq7xG
https://pastebin.com/qad7X0EQ
https://pastebin.com/G3xSFwem
https://pastebin.com/EffXNSx1
https://pastebin.com/e2GFCkaX
https://pastebin.com/d6XpErS5
https://pastebin.com/Atqhj6gz
https://pastebin.com/qmbXp0Sy
https://pastebin.com/UpgN3P4V
https://pastebin.com/VuwwY5ip
https://pastebin.com/NWnRfqPS
https://pastebin.com/c9GM9mBv
https://pastebin.com/0ZsBLMfd
https://pastebin.com/TFMs3tGt
https://pastebin.com/ehrjT1Gn
https://pastebin.com/m5RE6Avs
https://pastebin.com/fWYHdNgA
https://pastebin.com/gLVzuBx3
https://pastebin.com/rB9HYuaZ
https://pastebin.com/Fe2TBsvf
https://pastebin.com/xHugabcz
https://pastebin.com/ranMDSE7
https://pastebin.com/v1sqVnVc
https://pastebin.com/KB0TWNjm
https://pastebin.com/Z31VUeKU
https://pastebin.com/mjh7S1aF
https://pastebin.com/hrhppQs7
https://pastebin.com/gi9JXzNb
https://pastebin.com/fFVyxssb
https://pastebin.com/iBVj7uYr
https://pastebin.com/YvPJ8P5R
https://pastebin.com/70EAxttP
https://pastebin.com/EPbXeVtD
https://pastebin.com/EVSJNZin
https://pastebin.com/cc8QsGFx
https://pastebin.com/6TG7XnrV
https://pastebin.com/a84xV4uk
https://pastebin.com/KKZzVH4a
https://pastebin.com/XAMTJyHs
https://pastebin.com/Fz3DjJZ9
https://pastebin.com/g0sd0Bzs
https://pastebin.com/Jp8VpszT
https://pastebin.com/YUKEygx1
https://pastebin.com/uVwpBydg
https://pastebin.com/c9aAXgFc
https://pastebin.com/pZ28ZqPj
https://pastebin.com/rtW3W8AQ
https://pastebin.com/jg7cApH9
https://pastebin.com/JptcDPdH

0 关注 分享

要回复文章请先登录注册