首页
  • GM-3568JHF
  • M4-R1
  • M5-R1
  • SC-3568HA
  • M-K1HSE
  • CF-NRS1
  • CF-CRA2
  • 1684XB-32T
  • 1684X-416T
  • C-3568BQ
  • C-3588LQ
  • GC-3568JBAF
  • C-K1BA
商城
  • English
  • 简体中文
首页
  • GM-3568JHF
  • M4-R1
  • M5-R1
  • SC-3568HA
  • M-K1HSE
  • CF-NRS1
  • CF-CRA2
  • 1684XB-32T
  • 1684X-416T
  • C-3568BQ
  • C-3588LQ
  • GC-3568JBAF
  • C-K1BA
商城
  • English
  • 简体中文
  • M4-R1

    • 一、简介

      • M4-R1简介
    • 二、快速上手

      • 01 OpenHarmony概述
      • 02 镜像烧录
      • 03 应用开发快速上手
      • 04 设备开发快速上手
    • 三、应用开发

      • 01 ArkUI

        • 1 ArkTS语言简介
        • 2 UI 组件-Row 容器介绍
        • 3 UI 组件-Column 容器介绍
        • 4 UI 组件-Text 组件
        • 5 UI 组件-Toggle 组件
        • 6 UI 组件-Slider 组件
        • 7 UI 组件-Animation 组件&Transition 组件
      • 02 资料获取

        • 1 OpenHarmony 官方资料
      • 03 开发须知

        • 1 Full-SDK替换教程
        • 2 引入和使用三方库
        • 3 HDC调试
        • 4 命令行恢复出厂模式
        • 5 升级App为system权限
      • 04 构建第一个应用

        • 1 构建第一个ArkTs应用-HelloWorld
      • 05 案例

        • 01 串口调试助手应用案例
        • 02 手写板应用案例
        • 03 数字时钟应用案例
        • 04 WIFI 信息获取应用案例
    • 四、设备开发

      • 01 环境搭建
      • 02 下载源码
      • 03 编译源码
    • 五、外设与接口

      • 树莓派接口
      • GPIO 接口
      • I2C 接口
      • SPI通信
      • PWM控制
      • 串口通讯
      • TF Card
      • 屏幕
      • 触摸
      • 音频
      • RTC
      • Ethernet
      • M.2
      • MINI-PCIE
      • Camera
      • WIFI&BT
      • 树莓派拓展板
    • 六、资料下载

      • 资料下载
  • M5-R1

    • 一、简介

      • M5-R1简介
    • 二、快速上手

      • 镜像烧录
      • 环境搭建
      • 下载源码
    • 三、外设与接口

      • 树莓派接口
      • GPIO 接口
      • I2C 接口
      • SPI通信
      • PWM控制
      • 串口通讯
      • TF Card
      • 屏幕
      • 触摸
      • 音频
      • RTC
      • Ethernet
      • M.2
      • MINI-PCIE
      • Camera
      • WIFI&BT
    • 四、资料下载

      • 资料下载

4 UI 组件-Text 组件

文本组件是用于显示一段文本的基础组件,同时支持丰富的样式和布局配置,还可以包含 Span、ImageSpan 等子组件。

4.1 文本组件的定义与属性

接口:

Text(content?: string | Resource, options?: TextOptions)

属性说明:

  • content:显示的文本内容。
  • 样式设置:可以通过 .style() 方法为文本指定对齐方式、字体大小、边框、填充等样式。

4.2 使用示例

content:要显示的文本内容,一个简单的例子如下:

// xxx.ets
@Extend(Text)
function style(TextAlign: TextAlign) {
  .textAlign(TextAlign)
  .fontSize(12)
  .border({ width: 1 })
  .padding(10)
  .width('100%')
}

@Entry
@Component
struct TextExample1 {
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
      // 文本水平方向对齐方式设置
      // 单行文本
      Text('textAlign').fontSize(9).fontColor(0xCCCCCC)
      Text('TextAlign set to Center.')
        .style(TextAlign.Center)
      Text('TextAlign set to Start.')
        .style(TextAlign.Start)
      Text('TextAlign set to End.')
        .style(TextAlign.End)

      // 多行文本
      Text('This is the text content with textAlign set to Center.')
        .style(TextAlign.Center)
      Text('This is the text content with textAlign set to Start.')
        .style(TextAlign.Start)
      Text('This is the text content with textAlign set to End.')
        .style(TextAlign.End)


      // 文本超长时显示方式
      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 })
        .maxLines(1)
        .style(TextAlign.Start)

      // 超出maxLines展示省略号
      Text('This is set textOverflow to Ellipsis text content This is set textOverflow to Ellipsis text content.')
        .textOverflow({ overflow: TextOverflow.Ellipsis })
        .maxLines(1)
        .style(TextAlign.Start)

      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.')
        .style(TextAlign.Start)
      Text('This is the text with the line height set. This is the text with the line height set.')
        .style(TextAlign.Start)
        .lineHeight(20)
    }.height(600).width(340).padding({ left: 35, right: 35, top: 35 })
  }
}
在 GitHub 上编辑此页
上次更新:
贡献者: hjf, jxc
Prev
3 UI 组件-Column 容器介绍
Next
5 UI 组件-Toggle 组件