文本显示(Text/Span)

2024-01-25 13:15 更新

Text是文本组件,通常用于展示用户的视图,如显示文章的文字。具体用法可参考Text

创建文本

Text可通过以下两种方式来创建:

  • string字符串
    1. Text('我是一段文本')

  • 引用Resource资源

    资源引用类型可以通过$r创建Resource类型对象,文件位置为/resources/base/element/string.json

    1. Text($r('app.string.module_desc'))
    2. .baselineOffset(0)
    3. .fontSize(30)
    4. .border({ width: 1 })
    5. .padding(10)
    6. .width(300)

添加子组件

Span只能作为Text组件的子组件显示文本内容。可以在一个Text内添加多个Span来显示一段信息,例如产品说明书、承诺书等。

  • 创建Span。

    Span组件需要写到Text组件内,单独写Span组件不会显示信息,Text与Span同时配置文本内容时,Span内容覆盖Text内容。

    1. Text('我是Text') {
    2. Span('我是Span')
    3. }
    4. .padding(10)
    5. .borderWidth(1)

  • 设置文本装饰线及颜色。

    通过decoration设置文本装饰线及颜色。

    1. Text() {
    2. Span('我是Span1,').fontSize(16).fontColor(Color.Grey)
    3. .decoration({ type: TextDecorationType.LineThrough, color: Color.Red })
    4. Span('我是Span2').fontColor(Color.Blue).fontSize(16)
    5. .fontStyle(FontStyle.Italic)
    6. .decoration({ type: TextDecorationType.Underline, color: Color.Black })
    7. Span(',我是Span3').fontSize(16).fontColor(Color.Grey)
    8. .decoration({ type: TextDecorationType.Overline, color: Color.Green })
    9. }
    10. .borderWidth(1)
    11. .padding(10)

  • 通过textCase设置文字一直保持大写或者小写状态。
    1. Text() {
    2. Span('I am Upper-span').fontSize(12)
    3. .textCase(TextCase.UpperCase)
    4. }
    5. .borderWidth(1)
    6. .padding(10)

  • 添加事件。

    由于Span组件无尺寸信息,事件仅支持点击事件onClick。

    1. Text() {
    2. Span('I am Upper-span').fontSize(12)
    3. .textCase(TextCase.UpperCase)
    4. .onClick(()=>{
    5. console.info('我是Span——onClick')
    6. })
    7. }

自定义文本样式

  • 通过textAlign属性设置文本对齐样式。
    1. Text('左对齐')
    2. .width(300)
    3. .textAlign(TextAlign.Start)
    4. .border({ width: 1 })
    5. .padding(10)
    6. Text('中间对齐')
    7. .width(300)
    8. .textAlign(TextAlign.Center)
    9. .border({ width: 1 })
    10. .padding(10)
    11. Text('右对齐')
    12. .width(300)
    13. .textAlign(TextAlign.End)
    14. .border({ width: 1 })
    15. .padding(10)

  • 通过textOverflow属性控制文本超长处理,textOverflow需配合maxLines一起使用(默认情况下文本自动折行)。
    1. 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.')
    2. .width(250)
    3. .textOverflow({ overflow: TextOverflow.None })
    4. .maxLines(1)
    5. .fontSize(12)
    6. .border({ width: 1 }).padding(10)
    7. Text('我是超长文本,超出的部分显示省略号。I am an extra long text, with ellipses displayed for any excess。')
    8. .width(250)
    9. .textOverflow({ overflow: TextOverflow.Ellipsis })
    10. .maxLines(1)
    11. .fontSize(12)
    12. .border({ width: 1 }).padding(10)

  • 通过lineHeight属性设置文本行高。
    1. Text('This is the text with the line height set. This is the text with the line height set.')
    2. .width(300).fontSize(12).border({ width: 1 }).padding(10)
    3. Text('This is the text with the line height set. This is the text with the line height set.')
    4. .width(300).fontSize(12).border({ width: 1 }).padding(10)
    5. .lineHeight(20)

  • 通过decoration属性设置文本装饰线样式及其颜色
    1. Text('This is the text')
    2. .decoration({
    3. type: TextDecorationType.LineThrough,
    4. color: Color.Red
    5. })
    6. .borderWidth(1).padding(10).margin(5)
    7. Text('This is the text')
    8. .decoration({
    9. type: TextDecorationType.Overline,
    10. color: Color.Red
    11. })
    12. .borderWidth(1).padding(10).margin(5)
    13. Text('This is the text')
    14. .decoration({
    15. type: TextDecorationType.Underline,
    16. color: Color.Red
    17. })
    18. .borderWidth(1).padding(10).margin(5)

  • 通过baselineOffset属性设置文本基线的偏移量
    1. Text('This is the text content with baselineOffset 0.')
    2. .baselineOffset(0)
    3. .fontSize(12)
    4. .border({ width: 1 })
    5. .padding(10)
    6. .width('100%')
    7. .margin(5)
    8. Text('This is the text content with baselineOffset 30.')
    9. .baselineOffset(30)
    10. .fontSize(12)
    11. .border({ width: 1 })
    12. .padding(10)
    13. .width('100%')
    14. .margin(5)
    15. Text('This is the text content with baselineOffset -20.')
    16. .baselineOffset(-20)
    17. .fontSize(12)
    18. .border({ width: 1 })
    19. .padding(10)
    20. .width('100%')
    21. .margin(5)

  • 通过letterSpacing属性设置文本字符间距
    1. Text('This is the text content with letterSpacing 0.')
    2. .letterSpacing(0)
    3. .fontSize(12)
    4. .border({ width: 1 })
    5. .padding(10)
    6. .width('100%')
    7. .margin(5)
    8. Text('This is the text content with letterSpacing 3.')
    9. .letterSpacing(3)
    10. .fontSize(12)
    11. .border({ width: 1 })
    12. .padding(10)
    13. .width('100%')
    14. .margin(5)
    15. Text('This is the text content with letterSpacing -1.')
    16. .letterSpacing(-1)
    17. .fontSize(12)
    18. .border({ width: 1 })
    19. .padding(10)
    20. .width('100%')
    21. .margin(5)

  • 通过minFontSize与maxFontSize自适应字体大小,minFontSize设置文本最小显示字号maxFontSize设置文本最大显示字号,minFontSize与maxFontSize必须搭配同时使用,以及需配合maxline或布局大小限制一起使用,单独设置不生效
    1. Text('我的最大字号为30,最小字号为5,宽度为250,maxLines为1')
    2. .width(250)
    3. .maxLines(1)
    4. .maxFontSize(30)
    5. .minFontSize(5)
    6. .border({ width: 1 })
    7. .padding(10)
    8. .margin(5)
    9. Text('我的最大字号为30,最小字号为5,宽度为250,maxLines为2')
    10. .width(250)
    11. .maxLines(2)
    12. .maxFontSize(30)
    13. .minFontSize(5)
    14. .border({ width: 1 })
    15. .padding(10)
    16. .margin(5)
    17. Text('我的最大字号为30,最小字号为15,宽度为250,高度为50')
    18. .width(250)
    19. .height(50)
    20. .maxFontSize(30)
    21. .minFontSize(15)
    22. .border({ width: 1 })
    23. .padding(10)
    24. .margin(5)
    25. Text('我的最大字号为30,最小字号为15,宽度为250,高度为100')
    26. .width(250)
    27. .height(100)
    28. .maxFontSize(30)
    29. .minFontSize(15)
    30. .border({ width: 1 })
    31. .padding(10)
    32. .margin(5)

  • 通过textCase属性设置文本大小写。
    1. Text('This is the text content with textCase set to Normal.')
    2. .textCase(TextCase.Normal)
    3. .padding(10)
    4. .border({ width: 1 })
    5. .padding(10)
    6. .margin(5)
    7. // 文本全小写展示
    8. Text('This is the text content with textCase set to LowerCase.')
    9. .textCase(TextCase.LowerCase)
    10. .border({ width: 1 })
    11. .padding(10)
    12. .margin(5)
    13. // 文本全大写展示
    14. Text('This is the text content with textCase set to UpperCase.')
    15. .textCase(TextCase.UpperCase)
    16. .border({ width: 1 })
    17. .padding(10)
    18. .margin(5)

  • 通过copyOption属性设置文本是否可复制粘贴
    1. Text("这是一段可复制文本")
    2. .fontSize(30)
    3. .copyOption(CopyOptions.InApp)

添加事件

Text组件可以添加通用事件,可以绑定onClickonTouch等事件来响应操作。

  1. Text('点我')
  2. .onClick(()=>{
  3. console.info('我是Text的点击响应事件');
  4. })

场景示例

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct TextExample {
  5. build() {
  6. Column() {
  7. Row() {
  8. Text("1").fontSize(14).fontColor(Color.Red).margin({ left: 10, right: 10 })
  9. Text("我是热搜词条1")
  10. .fontSize(12)
  11. .fontColor(Color.Blue)
  12. .maxLines(1)
  13. .textOverflow({ overflow: TextOverflow.Ellipsis })
  14. .fontWeight(300)
  15. Text("爆")
  16. .margin({ left: 6 })
  17. .textAlign(TextAlign.Center)
  18. .fontSize(10)
  19. .fontColor(Color.White)
  20. .fontWeight(600)
  21. .backgroundColor(0x770100)
  22. .borderRadius(5)
  23. .width(15)
  24. .height(14)
  25. }.width('100%').margin(5)
  26. Row() {
  27. Text("2").fontSize(14).fontColor(Color.Red).margin({ left: 10, right: 10 })
  28. Text("我是热搜词条2 我是热搜词条2 我是热搜词条2 我是热搜词条2 我是热搜词条2")
  29. .fontSize(12)
  30. .fontColor(Color.Blue)
  31. .fontWeight(300)
  32. .constraintSize({ maxWidth: 200 })
  33. .maxLines(1)
  34. .textOverflow({ overflow: TextOverflow.Ellipsis })
  35. Text("热")
  36. .margin({ left: 6 })
  37. .textAlign(TextAlign.Center)
  38. .fontSize(10)
  39. .fontColor(Color.White)
  40. .fontWeight(600)
  41. .backgroundColor(0xCC5500)
  42. .borderRadius(5)
  43. .width(15)
  44. .height(14)
  45. }.width('100%').margin(5)
  46. Row() {
  47. Text("3").fontSize(14).fontColor(Color.Orange).margin({ left: 10, right: 10 })
  48. Text("我是热搜词条3")
  49. .fontSize(12)
  50. .fontColor(Color.Blue)
  51. .fontWeight(300)
  52. .maxLines(1)
  53. .constraintSize({ maxWidth: 200 })
  54. .textOverflow({ overflow: TextOverflow.Ellipsis })
  55. Text("热")
  56. .margin({ left: 6 })
  57. .textAlign(TextAlign.Center)
  58. .fontSize(10)
  59. .fontColor(Color.White)
  60. .fontWeight(600)
  61. .backgroundColor(0xCC5500)
  62. .borderRadius(5)
  63. .width(15)
  64. .height(14)
  65. }.width('100%').margin(5)
  66. Row() {
  67. Text("4").fontSize(14).fontColor(Color.Grey).margin({ left: 10, right: 10 })
  68. Text("我是热搜词条4 我是热搜词条4 我是热搜词条4 我是热搜词条4 我是热搜词条4")
  69. .fontSize(12)
  70. .fontColor(Color.Blue)
  71. .fontWeight(300)
  72. .constraintSize({ maxWidth: 200 })
  73. .maxLines(1)
  74. .textOverflow({ overflow: TextOverflow.Ellipsis })
  75. }.width('100%').margin(5)
  76. }.width('100%')
  77. }
  78. }

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号