HarmonyOS ArkUI Text 组件全解析:样式、截断与 Span 内联富文本
系列鸿蒙 HarmonyOS 6.1 新特性实战 · 第 31 篇Text 是 ArkUI 中使用频率最高的基础组件也是最容易被轻视的组件。掌握它的完整 API能让你用最少的代码写出专业的文字展示效果。本篇覆盖样式链、多行截断、Span 内联富文本、对齐行高四大核心用法代码均在鸿蒙模拟器验证通过。运行效果初始状态展示各类文字样式点击“展开全文“展开长文本切换 Overflow 模式一、基础文字样式链Text 的样式属性全部通过方法链调用支持流式叠加Text(加粗文字) .fontSize(16) .fontWeight(FontWeight.Bold) .fontColor(#333) .fontStyle(FontStyle.Italic) // 文字装饰线 Text(下划线).decoration({ type: TextDecorationType.Underline, color: #0066ff }) Text(删除线).decoration({ type: TextDecorationType.LineThrough, color: #e74c3c }) // 大写转换 Text(hello world) .textCase(TextCase.UpperCase) // → HELLO WORLDTextDecorationType的取值None、Underline、Overline、LineThrough。二、多行截断maxLines textOverflow超长文本的常见需求是“展开/收起“配合State控制行数State maxLines: number 2 State overflow: TextOverflow TextOverflow.Ellipsis Text(这是一段很长的演示文字……ArkTS 的 Text 组件支持单行和多行的文字截断……) .maxLines(this.maxLines) .textOverflow({ overflow: this.overflow }) .lineHeight(22) Button(this.maxLines 2 ? 展开全文 : 收起) .onClick(() { this.maxLines this.maxLines 2 ? 999 : 2 })TextOverflow的取值Ellipsis超出用…省略最常用Clip直接裁剪None不截断注意textOverflow必须配合maxLines才生效单独设置无效。三、Span 内联富文本当一段文字需要混合多种样式时用Span子组件替代多个TextText() { Span(普通文字 ) Span(蓝色加粗) .fontColor(#0066ff) .fontWeight(FontWeight.Bold) Span( 红色斜体) .fontColor(#e74c3c) .fontStyle(FontStyle.Italic) Span( 大字号) .fontSize(20) .fontColor(#27ae60) Span( 下划线) .decoration({ type: TextDecorationType.Underline, color: #8e44ad }) } .fontSize(15) .lineHeight(28)实战场景价格展示Text() { Span(价格) Span(¥).fontSize(14).fontColor(#e74c3c).fontWeight(FontWeight.Bold) Span(299).fontSize(24).fontColor(#e74c3c).fontWeight(FontWeight.Bold) Span(.00).fontSize(14).fontColor(#e74c3c) Span( 原价¥599).fontSize(12).fontColor(#aaa) .decoration({ type: TextDecorationType.LineThrough, color: #aaa }) }这种写法比多个Text横向排列更精确不会有换行问题。四、对齐与行高// 水平对齐 Text(居中).textAlign(TextAlign.Center).width(100%) Text(右对齐).textAlign(TextAlign.End).width(100%) // 行高提升阅读舒适度 Text(正文内容……) .lineHeight(32) .fontSize(14)TextAlign的取值Start默认、Center、End、JUSTIFY两端对齐。完整代码Entry Component struct Index { State maxLines: number 2 State overflow: TextOverflow TextOverflow.Ellipsis build() { Scroll() { Column({ space: 20 }) { Text(Text 组件全解析) .fontSize(22).fontWeight(FontWeight.Bold).fontColor(#1a1a1a) // 基础样式 Column({ space: 8 }) { Text(一、基础文字样式).fontSize(15).fontWeight(FontWeight.Bold).fontColor(#333) Text(普通文本fontSize16fontColor#333) .fontSize(16).fontColor(#333).width(100%) Text(加粗fontWeight Bold).fontSize(16).fontWeight(FontWeight.Bold).width(100%) Text(斜体fontStyle Italic).fontSize(16).fontStyle(FontStyle.Italic).width(100%) Text(下划线装饰).fontSize(16) .decoration({ type: TextDecorationType.Underline, color: #0066ff }).width(100%) Text(删除线).fontSize(16) .decoration({ type: TextDecorationType.LineThrough, color: #e74c3c }).width(100%) Text(大写转换 hello world).fontSize(14) .textCase(TextCase.UpperCase).fontColor(#888).width(100%) } .padding(12).backgroundColor(#fff).borderRadius(8).width(100%) .border({ width: 1, color: #e0e0e0 }).alignItems(HorizontalAlign.Start) // 多行截断 Column({ space: 8 }) { Text(二、多行截断).fontSize(15).fontWeight(FontWeight.Bold).fontColor(#333) Text(这是一段很长的演示文字用于展示 textOverflow 和 maxLines 的配合效果。ArkTS 的 Text 组件支持单行和多行的文字截断超出部分可以显示省略号或直接裁剪也可以通过 Marquee 实现跑马灯效果。) .fontSize(14).fontColor(#555).width(100%).lineHeight(22) .maxLines(this.maxLines) .textOverflow({ overflow: this.overflow }) Row({ space: 8 }) { Button(this.maxLines 2 ? 展开全文 : 收起) .fontSize(12).height(36).backgroundColor(#e8f0ff).fontColor(#0066ff) .onClick(() { this.maxLines this.maxLines 2 ? 999 : 2 }) Button(this.overflow TextOverflow.Ellipsis ? 改为 Clip : 改为省略号) .fontSize(12).height(36).backgroundColor(#e8f0ff).fontColor(#0066ff) .onClick(() { this.overflow this.overflow TextOverflow.Ellipsis ? TextOverflow.Clip : TextOverflow.Ellipsis }) } } .padding(12).backgroundColor(#fff).borderRadius(8).width(100%) .border({ width: 1, color: #e0e0e0 }).alignItems(HorizontalAlign.Start) // Span 内联富文本 Column({ space: 8 }) { Text(三、Span 内联富文本).fontSize(15).fontWeight(FontWeight.Bold).fontColor(#333) Text() { Span(普通文字 ) Span(蓝色加粗).fontColor(#0066ff).fontWeight(FontWeight.Bold) Span( 红色斜体).fontColor(#e74c3c).fontStyle(FontStyle.Italic) Span( 大字号).fontSize(20).fontColor(#27ae60) Span( 下划线).decoration({ type: TextDecorationType.Underline, color: #8e44ad }) } .fontSize(15).width(100%).lineHeight(28) Text() { Span(价格) Span(¥).fontSize(14).fontColor(#e74c3c).fontWeight(FontWeight.Bold) Span(299).fontSize(24).fontColor(#e74c3c).fontWeight(FontWeight.Bold) Span(.00).fontSize(14).fontColor(#e74c3c) Span( 原价¥599).fontSize(12).fontColor(#aaa) .decoration({ type: TextDecorationType.LineThrough, color: #aaa }) } .width(100%) } .padding(12).backgroundColor(#fff).borderRadius(8).width(100%) .border({ width: 1, color: #e0e0e0 }).alignItems(HorizontalAlign.Start) } .padding(20).width(100%) } .width(100%).height(100%).backgroundColor(#f5f5f5) } }API 速查属性/方法说明.fontSize(16)字号fp 单位.fontWeight(FontWeight.Bold)字重Lighter / Normal / Medium / Bold.fontColor(#333)字色.fontStyle(FontStyle.Italic)斜体.decoration({type, color})装饰线Underline / LineThrough / Overline.textCase(TextCase.UpperCase)大小写转换.maxLines(2)最大行数.textOverflow({overflow})超出截断方式.lineHeight(24)行高.textAlign(TextAlign.Center)水平对齐Text(){ Span(...) }内联混合样式小结样式全链式所有样式属性都支持链式调用无需 style 对象截断必须搭配textOverflow需要maxLines配合才生效Span 替代多 Text混合样式用 Span 子组件避免布局对齐问题行高改善阅读正文推荐lineHeight为fontSize的 1.5~2 倍上一篇AppStorage / PersistentStorage 全局状态管理 | 下一篇Image 组件完全指南objectFit、占位图与加载回调

相关新闻

HarmonyOS 页面路由:Router API

HarmonyOS 页面路由:Router API

适用版本:HarmonyOS 6.1(API 12)及以上 验证环境:Pura 90 Pro 模拟器(HarmonyOS 6.1.1,API 24) 关键概念:router.pushUrl、router.replaceUrl、router.back、路由栈、参数传递 前言 …

2026/10/24 15:04:27 阅读更多 →
突破性视觉自回归建模:VAR架构深度解析与性能优化指南

突破性视觉自回归建模:VAR架构深度解析与性能优化指南

突破性视觉自回归建模:VAR架构深度解析与性能优化指南 【免费下载链接】VAR [NeurIPS 2024 Best Paper Award][GPT beats diffusion🔥] [scaling laws in visual generation📈] Official impl. of "Visual Autoregressive Modeling: Sca…

2026/10/31 18:43:13 阅读更多 →
Siamese-pytorch训练技巧:5个实用技巧提升图片相似性检测准确率

Siamese-pytorch训练技巧:5个实用技巧提升图片相似性检测准确率

Siamese-pytorch训练技巧:5个实用技巧提升图片相似性检测准确率 【免费下载链接】Siamese-pytorch 这是一个孪生神经网络(Siamese network)的库,可进行图片的相似性比较。 项目地址: https://gitcode.com/gh_mirrors/si/Siamese…

2026/11/1 6:16:45 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/2 0:25:10 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/2 10:46:11 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/2 10:47:40 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/2 1:16:51 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/2 10:46:31 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/2 10:46:26 阅读更多 →

日新闻

周新闻

月新闻