Text 显示文本的控件,梳理一下文本显示的语法以及特殊需求
效果
- ... .textOverflow({ overflow: TextOverflow.Ellipsis })
- 跑马灯 .textOverflow({ overflow: TextOverflow.MARQUEE })
- Span Span(" SPAN2").fontColor(Color.Blue)
- 赋值粘贴.copyOption(CopyOptions.InApp)
一、基础文本样式
| 属性 | 类型 | 说明 | 示例 | |
|---|---|---|---|---|
content | string | Resource | 文本内容(可直接在 Text() 构造函数中传入) | Text('Hello World') |
fontSize | number | Resource | 字体大小(单位:vp) | .fontSize(20) |
fontColor | Color | Resource | 字体颜色(支持 HEX/RGB/颜色常量) | .fontColor('#FF0000') |
fontWeight | FontWeight | number | 字体粗细(如 FontWeight.Bold) | .fontWeight(500) |
fontStyle | FontStyle | 字体样式(Normal 或 Italic) | .fontStyle(FontStyle.Italic) | |
fontFamily | string | Resource | 字体家族(如 'Arial') | .fontFamily('HarmonyOS Sans') |
二、布局与尺寸控制
| 属性 | 类型 | 说明 | 示例 | ||
|---|---|---|---|---|---|
width | Length | Resource | 组件宽度(如 '100%', 200) | .width('80%') | |
height | Length | Resource | 组件高度 | .height(50) | |
maxLines | number | 最大显示行数(需配合 textOverflow) | .maxLines(2) | ||
textOverflow | {overflow: TextOverflow} | 文本溢出处理(Clip/Ellipsis/None) | .textOverflow({overflow: TextOverflow.Ellipsis}) | ||
lineHeight | string | number | Resource | 行高(单位:vp 或百分比) | .lineHeight('150%') |
textAlign | TextAlign | 水平对齐方式(Start/Center/End) | .textAlign(TextAlign.Center) | ||
textIndent | number | string | Resource | 首行缩进(单位:vp) | .textIndent(20) |
三、文本装饰与效果
| 属性 | 类型 | 说明 | 示例 | ||
|---|---|---|---|---|---|
decoration | { type: TextDecorationType, color?: Color } | 文本装饰线(Underline/Overline/LineThrough/None) | .decoration({ type: TextDecorationType.Underline, color: '#000' }) | ||
letterSpacing | number | string | Resource | 字符间距(单位:vp) | .letterSpacing(2) |
textCase | TextCase | 文本大小写转换(Normal/LowerCase/UpperCase) | .textCase(TextCase.UpperCase) | ||
baselineOffset | number | string | Resource | 基线偏移(单位:vp) | .baselineOffset(10) |
四、高级功能
| 属性 | 类型 | 说明 | 示例 |
|---|---|---|---|
onClick | (event: ClickEvent) => void | 点击事件回调 | .onClick(() => { ... }) |
copyOption | CopyOptions | 文本复制权限(None/Local/Distributed) | .copyOption(CopyOptions.Local) |
span | Span 数组 | 富文本(支持混合样式) | 见下方代码示例 |
五、富文本示例
typescript
复制
Text()
.span('红色文字', { fontSize: 20, fontColor: Color.Red })
.span(' + 蓝色文字', { fontSize: 16, fontColor: Color.Blue })
六、注意事项
-
默认值:
maxLines:Infinity(不限制行数)textAlign: 根据语言自动对齐(中文左对齐,阿拉伯语右对齐)
-
依赖关系:
maxLines必须与textOverflow配合使用才能显示省略号。textIndent仅在多行文本首行生效。
-
性能优化:
- 避免在长文本中频繁动态修改
span内容。 - 使用
textCase代替手动转换大小写以减少逻辑代码。
- 避免在长文本中频繁动态修改
-
版本兼容性:
copyOption需 HarmonyOS V3.1 或更高版本。
import { common } from "@kit.AbilityKit" import { promptAction } from "@kit.ArkUI" import { ScreenUtil } from "lib_util"
@Builder export function TestPageBuilder() { TestPage() }
/**
- 线性布局 */ @ComponentV2 struct TestPage {
//控制器 private controller: VideoController = new VideoController() // 占位位图 private previewUris: Resource = $r('app.media.ic_launcher') private scroller: Scroller = new Scroller(); @Local private textContent: string = '这是一个Text组件示例'; build() {
NavDestination() {
Column(){
Text(" 此类型按钮的圆角自动设置为高度的一半,不支持通过borderRadius属性重新设置圆角")
.backgroundColor(Color.Gray)
.textOverflow({ overflow: TextOverflow.Ellipsis })
.maxLines(1)
.copyOption(CopyOptions.InApp)
.fontColor("#000000") //文字颜色
.fontSize(22)// 文字大小
.width(200)
Text(" 此类型按钮的圆角自动设置为高度的一半,不支持通过borderRadius属性重新设置圆角")
.backgroundColor(Color.Gray)
.textOverflow({ overflow: TextOverflow.Clip })
.maxLines(1)
.fontColor("#000000") //文字颜色
.fontSize(22)// 文字大小
.width(200)
Text(" 此类型按钮的圆角自动设置为高度的一半,不支持通过borderRadius属性重新设置圆角")
.backgroundColor(Color.Gray)
.maxLines(1)
.textOverflow({ overflow: TextOverflow.MARQUEE })
.fontColor("#000000") //文字颜色
.fontSize(22)// 文字大小
.width(200)
Stack() {
Column({ space: 20 }) {
// 基础文本样式展示
Text(this.textContent)
.fontSize(20)
.fontColor('#0000FF')
.fontWeight(500)
.fontStyle(FontStyle.Normal)
.fontFamily('HarmonyOS Sans')
.width('80%')
// 布局与尺寸控制展示
Text('设置了最大行数和溢出样式的文本')
.maxLines(2)
.textOverflow({ overflow: TextOverflow.Ellipsis })
.width('80%')
.lineHeight('150%')
.textAlign(TextAlign.Center)
.textIndent(20)
// 文本装饰与效果展示
Text('带装饰线和字符间距的文本')
.decoration({ type: TextDecorationType.Underline, color: '#FF0000' })
.letterSpacing(2)
.textCase(TextCase.UpperCase)
.baselineOffset(5)
.width('80%')
// 点击事件和复制权限展示
Text('可点击和可本地复制的文本')
.onClick(() => {
console.log('文本被点击了');
})
.copyOption(CopyOptions.InApp)
.width('80%')
// 富文本(Span)展示
Text(){
Span("SPAN1")
Span(" SPAN2").fontColor(Color.Blue).backgroundColor(Color.Yellow)
Span(" SPAN3").fontColor(Color.Red).backgroundColor(Color.Yellow)
Span(" SPAN4").fontColor(Color.Gray).backgroundColor(Color.Yellow)
}
.width('80%')
}}}
.padding({top: px2vp(ScreenUtil.getInstance().getTopRectHeight()),bottom: px2vp(ScreenUtil.getInstance().getBottomRectHeight())})
.width("100%")
.height("100%")
// Scroll(this.scroller) {
// Column({ space: "35" }) {
// Text("11").width("100%").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
// Row() {
// Text("水平").width("100").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
// Blank()
// Toggle({ type: ToggleType.Checkbox, isOn: true })
// }.width("100%")
//
// Row({ space: "35" }) {
// Row() {
// Text("等分")
// .backgroundColor("#333333")
// .fontColor("#fff")
// .textAlign(TextAlign.Center)
// .align(Alignment.Center)
// .width("100%")
// .height("100")
// }.layoutWeight(1).backgroundColor(Color.Blue)
//
// Row() {
// Text("等分")
// .backgroundColor("#333333")
// .fontColor("#fff")
// .textAlign(TextAlign.Center)
// .align(Alignment.Center)
// .width("100%")
// .height("100")
// }.layoutWeight(1).backgroundColor(Color.Blue)
//
// Row() {
// Text("等分")
// .backgroundColor("#333333")
// .fontColor("#fff")
// .textAlign(TextAlign.Center)
// .align(Alignment.Center)
// .width("100%")
// .height("100")
// }.layoutWeight(1).backgroundColor(Color.Blue)
// }.width("100%")
//
// Text("11").width("100%").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
// Text("11").width("100%").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
// Text("11").width("100%").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
// Text("11").width("100%").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
// Text("11").width("100%").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
// Text("11").width("100%").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
// Text("11").width("100%").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
// Text("11").width("100%").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
// Text("11").width("100%").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
// Text("11").width("100%").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
// Text("11").width("100%").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
// Text("11").width("100%").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
// Text("11").width("100%").backgroundColor("#333333").fontColor("#fff").textAlign(TextAlign.Center)
//
// }.margin({right:20})
// }
// .backgroundColor(0xDCDCDC)
// .scrollable(ScrollDirection.Vertical) // 滚动方向为垂直方向
// .scrollBar(BarState.On) // 滚动条常驻显示
// .scrollBarColor(Color.Gray) // 滚动条颜色
// .scrollBarWidth(10) // 滚动条宽度
// .edgeEffect(EdgeEffect.Spring) // 滚动到边沿后回弹
// .width("100%").height("100%")
// .padding({
// top: px2vp(ScreenUtil.getInstance().getTopRectHeight()),
// bottom: px2vp(ScreenUtil.getInstance().getBottomRectHeight())
// })
}
.hideTitleBar(true)
.mode(NavDestinationMode.STANDARD)
// .onBackPressed(() => {
// (getContext(this) as common.UIAbilityContext)?.terminateSelf();
// return true
// })
} }