第九节HarmonyOS 常用基础组件1-Text

news/2024/7/21 8:48:41 标签: harmonyos, 华为

一、组件介绍

        组件(Component)是界面搭建与显示的最小单位,HarmonyOS ArkUI声名式为开发者提供了丰富多样的UI组件,我们可以使用这些组件轻松的编写出更加丰富、漂亮的界面。

        组件根据功能可以分为以下五大类:基础组件、容器组件、媒体组件、绘制组件、画布组件。其中基础组件时视图层的基本组成单位,包括Text、Image、TextInput、Buttton、LoadingProgress等。

下面我们将分别介绍这些常用基础组件的使用:

  • Text

Text组件用于在界面上展示一段文本信息,可以包含子组件Span。

  1. 文本样式

        针对包含文本元素的组件,例如Text、Span、Button、TextInput等,可使用fontColor、fontSize、fontStyle、 fontWeight、fontFamily这些文本样式,分别设置文本的颜色、大小、样式、粗细以及字体,文本样式的属性如下表:

名称

参数类型

描述

fontColor

ResourceColor

设置文本颜色。

fontSize

Length | Resource

设置文本尺寸,Length为number类型时,使用fp单位。

fontStyle

FontStyle

设置文本的字体样式。默认值:FontStyle.Normal。

fontWeight

number | FontWeight | string

设置文本的字体粗细,number类型取值[100, 900],取值间隔为100,默认为400,取值越大,字体越粗。string类型仅支持number类型取值的字符串形式,例如“400”,以及“bold”、“bolder”、“lighter”、“regular”、“medium”,分别对应FontWeight中相应的枚举值。默认值:FontWeight.Normal。

fontFamily

string | Resource

设置文本的字体列表。使用多个字体,使用“,”进行分割,优先级按顺序生效。例如:“Arial,sans-serif”。

示例代码中包含两个Text组件,第一个使用的是默认样式,第二、三个自己给文本设置了一些文本样式。

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text('HarmonyOS')
        Text('HarmonyOS')
          .fontColor(Color.Red)
          .fontSize(22)
          .fontStyle(FontStyle.Italic)
          .fontWeight(FontWeight.Bold)
          .fontFamily('Arial')
        Text('HarmonyOS')
          .fontColor(Color.Green)
          .fontSize(26)
          .fontStyle(FontStyle.Normal)
          .fontWeight(700)
          .fontFamily('sans-serif')
      }
      .width('100%')
    }
    .height('100%')
  }
}

2、设置文本对齐方式

使用textAlign属性可以设置文本的对齐方式,示例代码如下:

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text('HarmonyOS')
          .width(200)
          .backgroundColor(Color.Yellow)
          .textAlign(TextAlign.Start)
        Text('HarmonyOS')
          .fontColor(Color.Red)
          .fontSize(22)
          .fontStyle(FontStyle.Italic)
          .fontWeight(FontWeight.Bold)
          .fontFamily('Arial')
          .width(200)
          .margin({ top: 12 })
          .backgroundColor(Color.Yellow)
          .textAlign(TextAlign.Center)
        Text('HarmonyOS')
          .fontColor(Color.Green)
          .fontSize(26)
          .fontStyle(FontStyle.Normal)
          .fontWeight(700)
          .fontFamily('sans-serif')
          .width(200)
          .margin({ top: 12 })
          .backgroundColor(Color.Yellow)
          .textAlign(TextAlign.End)
      }
      .width('100%')
    }
    .height('100%')
  }
}

textAlign参数类型为TextAlign,定义了以下几种类型:

  • Start(默认值):水平对齐首部。

  • Center:水平居中对齐。

  • End:水平对齐尾部。

3、设置文本超长显示

        当文本内容较多超出了Text组件范围的时候,您可以使用textOverflow设置文本截取方式,需配合maxLines使用,单独设置不生效,maxLines用于设置文本显示最大行数。下面的示例代码将textOverflow设置为Ellipsis ,它将显示不下的文本用 “...” 表示:

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text('This is the text content of Text Component This is the text content of Text Component')
        Blank(10)
        Text('This is the text content of Text Component This is the text content of Text Component')
          .fontSize(16)
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
          .backgroundColor(0xE6F2EE)

      }
      .width('100%')
    }
    .height('100%')
  }
}

效果图如下:

4、设置文本装饰线

        使用decoration设置文本装饰线样式及其颜色,大家在浏览网页的时候经常可以看到装饰线,例如带有下划线超链接文本。

        decoration包含type和color两个参数,其中type用于设置装饰线样式,参数类型为TextDecorationTyp,color为可选参数。

下面的示例代码给文本设置了下划线,下划线颜色为黑色:

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text('HarmonyOS')
          .fontSize(20)
          .decoration({ type: TextDecorationType.Underline, color: Color.Black })
          .backgroundColor(Color.Gray)

      }
      .width('100%')
    }
    .height('100%')
  }
}

效果图如下:

TextDecorationTyp包含以下几种类型:

  • None:不使用文本装饰线。
    .decoration({ type: TextDecorationType.None, color: Color.Red })
  • Overline:文字上划线修饰。
.decoration({ type: TextDecorationType.Overline, color: Color.Red })

  • LineThrough:穿过文本的修饰线。
.decoration({ type: TextDecorationType.LineThrough, color: Color.Red })

  • Underline:文字下划线修饰。
.decoration({ type: TextDecorationType.Underline, color: Color.Red })


http://www.niftyadmin.cn/n/5232535.html

相关文章

RedisTemplate序列化的问题

1. 问题 在使用RedisTemplate 保存Set类型的数据时候出现保存的key和value被序列化了, public long sSet(String key, String... values) {try {return redisTemplate.opsForSet().add(key, values);} catch (Exception e) {LOGGER.error("异常{}", e);…

TCP简介及特性

1. TCP协议简介 TCP是Transmission Control Protocol的简称,中文名是传输控制协议。它是一种面向连接的、可靠的、基于IP的传输层协议。两个TCP应用之间在传输数据的之前必须建立一个TCP连接,TCP采用数据流的形式在网络中传输数据。TCP为了保证报文传输的…

CityEngine2023安装与快速入门

目录 0 引言1 安装2 基本操作3 CityEngine官方示例3.1 官方地址3.2 导入示例工程 3 结尾 🙋‍♂️ 作者:海码007📜 专栏:CityEngine专栏💥 标题:CityEngine2023安装与快速入门❣️ 寄语:书到用时…

【CTA认证】Android CTA资料及信息安全要求

认证资料需求 1. 说明书;需有应用场景、使用人群说明 2. 产品铭牌(需有IMEI号,产品名称需是:TD-LTE无线数据终端) 3. 产品整体尺寸长宽高 4. 原理框图; 5. 主板正反面照片(需拆除屏蔽罩&a…

sd_webui的实用插件,prompt/lama/human matting/...

热烈欢迎大家在git上star!!!冲鸭!!! 1.prompt优化插件 GitHub - leeguandong/sd_webui_beautifulprompt: beautifulprompt extension performs stable diffusion automatic prompt engineering on a bro…

【Python】永久切换pip下载源

目录 永久切换pip下载源切换方式pip国内镜像源参考 永久切换pip下载源 使用Python默认pip下载源容易导致部分库下载超时失败,将pip下载源切换回国内,避免超时访问导致失败 切换方式 通过修改配置文件完成切换: 进入到 C:\Users\用户名 路径…

【蓝桥杯软件赛 零基础备赛20周】第5周——高精度大数运算与队列

文章目录 1. 数组的应用–高精度大数运算1.1 Java和Python计算大数1.2 C/C高精度计算大数1.2.1 高精度加法1.2.2 高精度减法 2. 队列2.1 手写队列2.1.1 C/C手写队列2.1.2 Java手写队列2.1.3 Python手写队列 2.2 C STL队列queue2.3 Java队列Queue2.4 Python队列Queue和deque2.5 …

视频怎么去水印?如何下载保存无水印视频?

你是否曾经在观看鬼畜素材视频时,被烦人的水印挡住了视线,让你感到十分郁闷?不要担心,今天我将为你介绍几种经典的方法,让你轻松下载无水印视频,让观看体验更加清爽不留痕迹。让我们一起来试试吧&#xff0…