1、Text 组件基本操作、用法
使用方法、细节都在代码注释中
2、源代码
@Entry // 使用 @Entry 装饰器标识这是一个入口组件。
@Component // 使用 @Component 装饰器定义一个新组件。
struct TextExample1 { // 定义名为 TextExample1 的结构体,代表这个组件。
build() { // 定义 build 方法来构建UI。
Flex({ // 创建一个弹性布局容器。
direction: FlexDirection.Column, // 设置布局方向为垂直列。
alignItems: ItemAlign.Start, // 设置子项沿主轴的起始位置对齐。
justifyContent: FlexAlign.SpaceBetween // 设置子项间距均匀分布。
}) {
// 文本水平方向对齐方式设置
// 单行文本
Text('textAlign').fontSize(9).fontColor(0xCCCCCC) // 创建一个文本组件,说明接下来的文本对齐设置。
Text('TextAlign set to Center.') // 创建一个文本组件,文本居中对齐。
.textAlign(TextAlign.Center) // 设置文本对齐方式为居中。
.fontSize(12) // 设置字体大小为12。
.border({ width: 1 }) // 设置边框宽度为1。
.padding(10) // 设置内边距为10。
.width('100%') // 设置宽度为100%。
Text('TextAlign set to Start.') // 创建一个文本组件,文本起始对齐。
.textAlign(TextAlign.Start) // 设置文本对齐方式为起始对齐。
.fontSize(12) // 设置字体大小为12。
.border({ width: 1 }) // 设置边框宽度为1。
.padding(10) // 设置内边距为10。
.width('100%') // 设置宽度为100%。
Text('TextAlign set to End.') // 创建一个文本组件,文本结束对齐。
.textAlign(TextAlign.End) // 设置文本对齐方式为结束对齐。
.fontSize(12) // 设置字体大小为12。
.border({ width: 1 }) // 设置边框宽度为1。
.padding(10) // 设置内边距为10。
.width('100%') // 设置宽度为100%。
// 多行文本
Text('This is the text content with textAlign set to Center.') // 创建一个多行文本组件,文本居中对齐。
.textAlign(TextAlign.Center) // 设置文本对齐方式为居中。
.fontSize(12) // 设置字体大小为12。
.border({ width: 1 }) // 设置边框宽度为1。
.padding(10) // 设置内边距为10。
.width('100%') // 设置宽度为100%。
Text('This is the text content with textAlign set to Start.') // 创建一个多行文本组件,文本起始对齐。
.textAlign(TextAlign.Start) // 设置文本对齐方式为起始对齐。
.fontSize(12) // 设置字体大小为12。
.border({ width: 1 }) // 设置边框宽度为1。
.padding(10) // 设置内边距为10。
.width('100%') // 设置宽度为100%。
Text('This is the text content with textAlign set to End.') // 创建一个多行文本组件,文本结束对齐。
.textAlign(TextAlign.End) // 设置文本对齐方式为结束对齐。
.fontSize(12) // 设置字体大小为12。
.border({ width: 1 }) // 设置边框宽度为1。
.padding(10) // 设置内边距为10。
.width('100%') // 设置宽度为100%。
// 文本超长时显示方式
Text('TextOverflow+maxLines').fontSize(9).fontColor(0xCCCCCC) // 创建一个文本组件,说明接下来的文本溢出设置。
// 超出maxLines截断内容展示
Text('This is the setting of textOverflow to Clip text content This is the setting of textOverflow to None text content. This is the setting of textOverflow to Clip text content This is the setting of textOverflow to None text content.')
.textOverflow({ overflow: TextOverflow.Clip }) // 设置文本溢出方式为剪裁(Clip)。
.maxLines(1) // 设置最大行数为1。
.fontSize(12) // 设置字体大小为12。
.border({ width: 1 }) // 设置边框宽度为1。
.padding(10) // 设置内边距为10。
// 超出maxLines展示省略号
Text('This is set textOverflow to Ellipsis text content This is set textOverflow to Ellipsis text content.'.split('')
.join('\u200B'))
.textOverflow({ overflow: TextOverflow.Ellipsis }) // 设置文本溢出方式为省略号(Ellipsis)。
.maxLines(1) // 设置最大行数为1。
.fontSize(12) // 设置字体大小为12。
.border({ width: 1 }) // 设置边框宽度为1。
.padding(10) // 设置内边距为10。
Text('lineHeight').fontSize(9).fontColor(0xCCCCCC) // 创建一个文本组件,说明接下来的行高设置。
// 设置文本的行高
Text('This is the text with the line height set. This is the text with the line height set.')
.fontSize(12) // 设置字体大小为12。
.border({ width: 1 }) // 设置边框宽度为1。
.padding(10) // 设置内边距为10。
Text('This is the text with the line height set. This is the text with the line height set.')
.fontSize(12) // 设置字体大小为12。
.border({ width: 1 }) // 设置边框宽度为1。
.padding(10) // 设置内边距为10。
.lineHeight(20) // 设置行高为20。
}.height(600).width(350).padding({ left: 35, right: 35, top: 35 }) // 设置容器的高度、宽度和内边距。
}
}
运行结果截图

封面图

本文详细介绍了如何在Text组件中实现文本的水平和垂直对齐、处理文本超长时的显示方式以及设置行高,通过实例展示了代码的使用方法和效果。
303

被折叠的 条评论
为什么被折叠?



