当前位置:   article > 正文

03. 快速上手!HarmonyOS4.0 Text/Span组件详解_鸿蒙 textspan index

鸿蒙 textspan index

目录

推荐阅读

     01.HarmonyOS4.0 工具安装 --- 启航篇https://ruocheng.blog.csdn.net/article/details/136640619

主要内容(思维导图)

1. 组件介绍

2. 组件的使用

3. Text组件详解

3.1. Text组件基础使用

3.1.1. TextAlign 属性

3.1.2. textOverflow

3.1.3. 文本字符间距/文本的文本行高

3.1.4. 其他属性

4. Span组件详解

4.1. Span组件基础使用

4.1.1. Span 组件单独使用

4.1.2. 配合Text 组件使用

4.1.3. Span 组件相关属性


推荐阅读

01. 从TypeScript入手,驾驭HarmonyOS开发的技术风潮!-------(上篇)icon-default.png?t=N7T8https://ruocheng.blog.csdn.net/article/details/136444192

02. 从TypeScript入手,驾驭HarmonyOS开发的技术风潮!-----------(中篇)icon-default.png?t=N7T8https://ruocheng.blog.csdn.net/article/details/13646833003. 从TypeScript入手,驾驭HarmonyOS开发的技术风潮!-----------(下篇)icon-default.png?t=N7T8https://ruocheng.blog.csdn.net/article/details/13655176304. 从TypeScript入手,驾驭HarmonyOS开发的技术风潮!-----------(番外篇)icon-default.png?t=N7T8https://ruocheng.blog.csdn.net/article/details/136615369

     01.HarmonyOS4.0 工具安装 --- 启航篇icon-default.png?t=N7T8https://ruocheng.blog.csdn.net/article/details/136640619

     02. 快速上手!HarmonyOS4.0 Image组件详解icon-default.png?t=N7T8https://ruocheng.blog.csdn.net/article/details/136669539

主要内容(思维导图

1. 组件介绍

显示一段文本的组件。

2. 组件的使用

3. Text组件详解

显示一段文本的组件, 可以包含Span子组件。

3.1. Text组件基础使用

3.1.1. TextAlign 属性
  1. /**
  2. * @Author: 若城
  3. * @Description:SpanNote
  4. */
  5. @Entry
  6. @Component
  7. struct SpanNote {
  8. @State message: string = 'Hello World'
  9. build() {
  10. Row() {
  11. Column() {
  12. Row() {
  13. Text('设置文本段落在水平方向的对齐方式').fontSize(30).fontColor('#ccc').padding(10)
  14. }.width('100%').justifyContent(FlexAlign.Start)
  15. Row({ space: 20 }) {
  16. Column() {
  17. Text('TextAlign 属性之 Start').textAlign(TextAlign.Start).fontSize(20).backgroundColor('#ccc').padding(10).margin({bottom:10}).width('100%')
  18. Text('TextAlign 属性之 Center').textAlign(TextAlign.Center).fontSize(20).backgroundColor('#ccc').padding(10).margin({bottom:10}).width('100%')
  19. Text('TextAlign 属性之 End').textAlign(TextAlign.End).fontSize(20).backgroundColor('#ccc').padding(10).margin({bottom:10}).width('100%')
  20. }.width('100%').alignItems(HorizontalAlign.Start)
  21. }.width('100%').justifyContent(FlexAlign.SpaceEvenly)
  22. }
  23. .width('100%')
  24. .height('100%')
  25. }
  26. .height('100%')
  27. }
  28. }

效果图展示

3.1.2. textOverflow

注意在使用textOverflow 属性时需要使用 maxLines 属性来设定文本显示行数

maxLines: 设置文本的最大行数。

  1. Row({ space: 20 }) {
  2. Column() {
  3. Text('文本超长时进行裁剪显示。').textOverflow({overflow:TextOverflow.Clip}).maxLines(1).fontSize(20).backgroundColor('#ccc').padding(10).margin({bottom:10}).width(200)
  4. Text('文本超长时显示不下的文本用省略号代替。').textOverflow({overflow:TextOverflow.Ellipsis}).maxLines(1).fontSize(20).backgroundColor('#ccc').padding(10).margin({bottom:10}).width(200)
  5. Text('文本超长时不进行裁剪。').textOverflow({overflow:TextOverflow.None}).maxLines(1).fontSize(20).backgroundColor('#ccc').padding(10).margin({bottom:10}).width(200)
  6. }.width('100%').alignItems(HorizontalAlign.Start)
  7. }.width('100%').justifyContent(FlexAlign.SpaceEvenly)

效果展示

3.1.3. 文本字符间距/文本的文本行高
  1. Row({ space: 20 }) {
  2. Column() {
  3. Text('设置文本字符间距。')
  4. .letterSpacing(20)
  5. .fontSize(20).backgroundColor('#ccc').padding(10).margin({bottom:10}).width(200)
  6. Text('设置文本的文本行高,设置文本的文本行高,')
  7. .lineHeight(40)
  8. .fontSize(20).backgroundColor('#ccc').padding(10).margin({bottom:10}).width(200)
  9. }.width('100%').alignItems(HorizontalAlign.Start)
  10. }.width('100%').justifyContent(FlexAlign.SpaceEvenly)

效果展示

3.1.4. 其他属性

下面展示的仅为上面没有讲到的内容,可做参考

4. Span组件详解

Span组件,作为Text组件的子组件是用于显示行内文本的组件。

4.1. Span组件基础使用

需要注意的是 Span 组件不可单独使用,需要配和Text 组件使用

4.1.1. Span 组件单独使用

代码案例

  1. /**
  2. * @Author: 若城
  3. * @Description:
  4. */
  5. @Entry
  6. @Component
  7. struct TextNote {
  8. @State message: string = 'Hello World'
  9. build() {
  10. Row() {
  11. Column() {
  12. Row() {
  13. Text('设置文本段落在水平方向的对齐方式').fontSize(30).fontColor('#ccc').padding(10)
  14. }.width('100%').justifyContent(FlexAlign.Start)
  15. Row({ space: 20 }) {
  16. Column() {
  17. Span('单独使用,文本不显示').fontSize(30)
  18. }.width('100%').alignItems(HorizontalAlign.Start)
  19. }.width('100%').justifyContent(FlexAlign.SpaceEvenly)
  20. }
  21. .width('100%')
  22. .height('100%')
  23. }
  24. .height('100%')
  25. }
  26. }

4.1.2. 配合Text 组件使用

代码案例

  1. /**
  2. * @Author: 若城
  3. * @Description:
  4. */
  5. @Entry
  6. @Component
  7. struct TextNote {
  8. @State message: string = 'Hello World'
  9. build() {
  10. Row() {
  11. Column() {
  12. Row() {
  13. Text('配合Text 组件使用').fontSize(30).fontColor('#ccc').padding(10)
  14. }.width('100%').justifyContent(FlexAlign.Start)
  15. Row({ space: 20 }) {
  16. Column() {
  17. Text(){
  18. Span('配合Text 组件使用//').fontSize(30)
  19. Span('多个行内组件').fontSize(30)
  20. }
  21. }.width('100%').alignItems(HorizontalAlign.Start)
  22. }.width('100%').justifyContent(FlexAlign.SpaceEvenly)
  23. }
  24. .width('100%')
  25. .height('100%')
  26. }
  27. .height('100%')
  28. }
  29. }

效果演示

注意: Span组件是行内元素

4.1.3. Span 组件相关属性

名称

类型

描述

默认值

支持版本

decoration

{type: TextDecorationType, color?: ResourceColor}

设置文本装饰线样式及其颜色

{type: TextDecorationType.None, color: Color.Black}

API version 9

letterSpacing

number | string

设置文本字符间距

-

API version 9

textCase

TextCase

设置文本大小写

TextCase.Normal

API version 9

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/887956?site
推荐阅读
相关标签
  

闽ICP备14008679号