HarmonyOS6 RcText 组件核心架构与类型系统设计
RcText 是 HarmonyOS6 下的企业级文本展示组件,基于 ComponentV2 装饰器构建。文章解析了其参数状态管理、类型系统设计及核心设计模式(策略、计算属性、适配器、装饰器、模板方法)。组件支持主题样式、尺寸规格、对齐方式及多种显示模式(价格、手机号脱敏等),遵循 SOLID 原则,提供类型安全保障与交互处理机制,旨在提升鸿蒙应用开发效率与代码可维护性。

RcText 是 HarmonyOS6 下的企业级文本展示组件,基于 ComponentV2 装饰器构建。文章解析了其参数状态管理、类型系统设计及核心设计模式(策略、计算属性、适配器、装饰器、模板方法)。组件支持主题样式、尺寸规格、对齐方式及多种显示模式(价格、手机号脱敏等),遵循 SOLID 原则,提供类型安全保障与交互处理机制,旨在提升鸿蒙应用开发效率与代码可维护性。

RcText 是一个功能强大的文本展示组件,采用 ComponentV2 装饰器实现,支持多种主题样式、尺寸规格、特殊显示模式以及灵活的样式定制。本文将深入解析 RcText 组件的核心架构设计、类型系统以及设计思想。
RcText 组件采用 HarmonyOS6 最新的 @ComponentV2 装饰器,这是 ArkUI 框架的新一代组件定义方式:
@ComponentV2
export struct RcText {
// 组件实现
}
核心优势:
组件使用 @Param 装饰器定义所有可配置属性,实现清晰的参数管理:
@Param text: string | number = ''
@Param type: RcTextType = 'default'
@Param textSize: RcTextSize = 'default'
这三个参数构成文本组件的核心:
@Param color: string | Resource = ''
@Param fontSize: RcStringNumber = 0
@Param fontWeight: RcTextWeight = 'normal'
@Param textAlign: RcTextAlign = 'left'
@Param textDecoration: RcTextDecoration = 'none'
@Param lineHeight: RcStringNumber = 0
设计亮点:
@Param maxLines: number = 0
@Param truncated: boolean = false
@Param mode: RcTextMode = 'text'
@Param href: string = ''
@Param selectable: boolean = false
这些参数提供了文本组件的高级能力:
RcText 组件建立了完善的类型定义体系,确保类型安全和开发体验。
export type RcTextType = 'default' | 'primary' | 'success' | 'warning' | 'danger' | 'info'
设计思路:
颜色映射关系:
| 类型 | 颜色值 | 使用场景 |
|---|---|---|
| default | #303133 | 常规文本内容 |
| primary | #409eff | 强调信息、链接 |
| success | #67c23a | 成功提示、正向状态 |
| warning | #e6a23c | 警告信息、注意事项 |
| danger | #f56c6c | 错误提示、危险操作 |
| info | #909399 | 次要信息、辅助说明 |
export type RcTextSize = 'large' | 'default' | 'small'
尺寸预设规范:
| 尺寸 | 字体大小 | 使用场景 |
|---|---|---|
| large | 18vp | 标题文字、重要信息 |
| default | 14vp | 正文内容、常规显示 |
| small | 12vp | 辅助文字、次要信息 |
设计考量:
export type RcTextAlign = 'left' | 'center' | 'right' | 'start' | 'end'
类型说明:
export type RcTextDecoration = 'none' | 'underline' | 'line-through'
装饰效果:
export type RcTextWeight = 'normal' | 'bold' | 'lighter' | 'bolder' | number
灵活设计:
export type RcTextMode = 'text' | 'price' | 'phone' | 'link'
这是 RcText 的创新设计,内置了常见的业务场景:
模式详解:
text 模式 (默认)
price 模式
1234.56 → ¥1,234.56phone 模式
13888888888 → 138****8888link 模式
组件采用策略模式管理不同主题的颜色配置:
private getTypeColor(): string | Resource {
if (this.color) {
return this.color
}
switch (this.type) {
case 'primary': return '#409eff'
case 'success': return '#67c23a'
case 'warning': return '#e6a23c'
case 'danger': return '#f56c6c'
case 'info': return '#909399'
case 'default': default: return '#303133'
}
}
设计亮点:
使用私有方法实现计算属性,处理尺寸的优先级逻辑:
private getSizeValue(): string {
if (this.fontSize) {
return getSizeByUnit(this.fontSize)
}
switch (this.textSize) {
case 'large': return '18vp'
case 'small': return '12vp'
case 'default': default: return '14vp'
}
}
优先级规则:
工具函数集成:
getSizeByUnit 函数统一处理数值单位将自定义的对齐类型转换为系统 TextAlign 枚举:
private getTextAlign(): TextAlign {
switch (this.textAlign) {
case 'left': return TextAlign.Start
case 'center': return TextAlign.Center
case 'right': return TextAlign.End
case 'start': return TextAlign.Start
case 'end': return TextAlign.End
default: return TextAlign.Start
}
}
设计意图:
转换文本装饰类型为系统枚举:
private getDecoration(): TextDecorationType {
switch (this.textDecoration) {
case 'underline': return TextDecorationType.Underline
case 'line-through': return TextDecorationType.LineThrough
case 'none': default: return TextDecorationType.None
}
}
配合使用示例:
.decoration({ type: this.getDecoration(), color: this.getTypeColor() })
装饰线颜色自动跟随文本颜色,保持视觉一致性。
getFormattedText 方法定义了文本格式化的算法骨架:
private getFormattedText(): string {
const textValue = String(this.text)
switch (this.mode) {
case 'price':
const num = parseFloat(textValue)
if (isNaN(num)) return textValue
return '¥' + num.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
case 'phone':
if (textValue.length === 11) {
return textValue.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
}
return textValue
case 'link': return textValue
case 'text': default: return textValue
}
}
实现细节:
toFixed(2): 保留两位小数\B: 非单词边界,避免开头添加逗号(?=(\d{3})+(?!\d)): 正向预查,匹配 3 的倍数位置容错处理:
手机号脱敏算法:
textValue.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
价格格式化算法:
num.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
RcText 使用 Row 布局实现横向排列:
build() {
Row() {
// 前置元素
// 文本内容
// 后置元素
}.margin(this.rcMargin).padding(this.rcPadding).onClick(() => this.handleClick())
}
布局优势:
组件支持可选的前后置元素:
// 前置部分
if (this.prefixIcon) {
RcIcon({ name: this.prefixIcon, iconSize: this.iconSize, color: this.iconColor || this.getTypeColor() }).margin({ right: 4 })
}
// 文本部分 (核心)
Text(this.getFormattedText()).fontSize(this.getSizeValue()).fontColor(this.getTypeColor())
// ...更多样式配置
// 后置部分
if (this.suffixIcon) {
RcIcon({ name: this.suffixIcon, iconSize: this.iconSize, color: this.iconColor || this.getTypeColor() }).margin({ left: 4 })
}
设计考量:
核心 Text 组件的属性配置体现了组件的灵活性:
Text(this.getFormattedText())
.fontSize(this.getSizeValue())
.fontColor(this.getTypeColor())
.fontWeight(this.getFontWeight())
.textAlign(this.getTextAlign())
.decoration({ type: this.getDecoration(), color: this.getTypeColor() })
.lineHeight(this.lineHeight ? getSizeByUnit(this.lineHeight) : undefined)
.maxLines(this.maxLines > 0 ? this.maxLines : (this.truncated ? 1 : undefined))
.textOverflow(this.maxLines > 0 || this.truncated ? { overflow: TextOverflow.Ellipsis } : undefined)
.copyOption(this.selectable ? CopyOptions.InApp : CopyOptions.)
.()
属性解析:
组件提供了统一的点击事件处理机制:
private handleClick() {
if (this.mode === 'link' && this.href) {
console.log('Open link:', this.href)
}
if (this.onTextClick) {
this.onTextClick()
}
}
处理流程:
扩展性设计:
Row 容器的 onClick 绑定确保整个组件区域可点击:
Row() {
// 内容
}.onClick(() => this.handleClick())
这种设计使得不仅文本可点击,整个组件区域都可以响应点击。
每个私有方法都有明确的单一职责:
getTypeColor(): 专注于颜色获取getSizeValue(): 专注于尺寸计算getFormattedText(): 专注于文本格式化getTextAlign(): 专注于对齐方式转换组件对扩展开放,对修改封闭:
Text 组件可以被 RcText 无缝替换:
组件的 Props 接口定义清晰:
组件依赖抽象而非具体实现:
组件使用了 RcStringNumber 工具类型:
export type RcStringNumber = string | number
这个类型在 rchoui 组件库中广泛使用,统一处理尺寸参数的多种输入格式。
大量使用联合类型提供类型约束:
@Param type: RcTextType = 'default'
@Param textSize: RcTextSize = 'default'
优势:
使用可选类型提供灵活性:
@Param color: string | Resource = ''
空字符串作为默认值,未设置时使用 type 对应的颜色。
RcText 组件通过精心的架构设计实现了:6 种主题、3 种尺寸、多种对齐和装饰方式,支持预设配置和自定义样式的完美结合,预留扩展点,支持主题定制和功能增强,遵循设计原则,易于理解和维护。
组件的成功在于找到了'简单易用'和'功能强大'之间的平衡点,既满足快速开发需求,又提供深度定制能力。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online
将JSON字符串修饰为友好的可读格式。 在线工具,JSON美化和格式化在线工具,online