文本的組件

2024-01-22 17:28 更新

顯示一段文本的組件。

說明

該組件從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標單獨標記該內(nèi)容的起始版本。

子組件

可以包含Span子組件。

接口

Text(content?: string | Resource)

從API version 9開始,該接口支持在ArkTS卡片中使用。

參數(shù):

參數(shù)名

參數(shù)類型

必填

參數(shù)描述

content

string | Resource

文本內(nèi)容。包含子組件Span時不生效,顯示Span內(nèi)容,并且此時text組件的樣式不生效。

默認值:' '

屬性

除支持通用屬性外,還支持以下屬性:

名稱

參數(shù)類型

描述

textAlign

TextAlign

設置文本段落在水平方向的對齊方式

默認值:TextAlign.Start

說明:

文本段落寬度占滿Text組件寬度。

可通過align屬性控制文本段落在垂直方向上的位置,此組件中不可通過align屬性控制文本段落在水平方向上的位置,即align屬性中Alignment.TopStart、Alignment.Top、Alignment.TopEnd效果相同,控制內(nèi)容在頂部。Alignment.Start、Alignment.Center、Alignment.End效果相同,控制內(nèi)容垂直居中。Alignment.BottomStart、Alignment.Bottom、Alignment.BottomEnd效果相同,控制內(nèi)容在底部。結合TextAlign屬性可控制內(nèi)容在水平方向的位置。

從API version 9開始,該接口支持在ArkTS卡片中使用。

textOverflow

{overflow: TextOverflow}

設置文本超長時的顯示方式。

默認值:{overflow: TextOverflow.Clip}

說明:

文本截斷是按字截斷。例如,英文以單詞為最小單位進行截斷,若需要以字母為單位進行截斷,可在字母間添加零寬空格:\u200B。

需配合maxLines使用,單獨設置不生效。

從API version 9開始,該接口支持在ArkTS卡片中使用。

maxLines

number

設置文本的最大行數(shù)。

說明:

默認情況下,文本是自動折行的,如果指定此參數(shù),則文本最多不會超過指定的行。如果有多余的文本,可以通過 textOverflow來指定截斷方式。

從API version 9開始,該接口支持在ArkTS卡片中使用。

lineHeight

string | number | Resource

設置文本的文本行高,設置值不大于0時,不限制文本行高,自適應字體大小,Length為number類型時單位為fp。

從API version 9開始,該接口支持在ArkTS卡片中使用。

decoration

{

type: TextDecorationType,

color?: ResourceColor

}

設置文本裝飾線樣式及其顏色。

默認值:{

type: TextDecorationType.None,

color:Color.Black

}

從API version 9開始,該接口支持在ArkTS卡片中使用。

baselineOffset

number | string

設置文本基線的偏移量,默認值0。

從API version 9開始,該接口支持在ArkTS卡片中使用。

說明:

設置該值為百分比時,按默認值顯示。

letterSpacing

number | string

設置文本字符間距。

從API version 9開始,該接口支持在ArkTS卡片中使用。

說明:

設置該值為百分比時,按默認值顯示。

minFontSize

number | string | Resource

設置文本最小顯示字號。

需配合maxFontSize以及maxline或布局大小限制使用,單獨設置不生效。

從API version 9開始,該接口支持在ArkTS卡片中使用。

maxFontSize

number | string | Resource

設置文本最大顯示字號。

需配合minFontSize以及maxline或布局大小限制使用,單獨設置不生效。

從API version 9開始,該接口支持在ArkTS卡片中使用。

textCase

TextCase

設置文本大小寫。

默認值:TextCase.Normal

從API version 9開始,該接口支持在ArkTS卡片中使用。

copyOption9+

CopyOptions

組件支持設置文本是否可復制粘貼。

默認值:CopyOptions.None

該接口支持在ArkTS卡片中使用。

說明:

設置copyOptions為CopyOptions.InApp或者CopyOptions.LocalDevice,長按文本,會彈出文本選擇菜單,可選中文本并進行復制、全選操作。

說明

不支持Text內(nèi)同時存在文本內(nèi)容和Span子組件。如果同時存在,只顯示Span內(nèi)的內(nèi)容。

事件

支持通用事件

示例

示例1

textAlign,textOverflow,maxLines,lineHeight使用示例。

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct TextExample1 {
  5. build() {
  6. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
  7. // 文本水平方向對齊方式設置
  8. // 單行文本
  9. Text('textAlign').fontSize(9).fontColor(0xCCCCCC)
  10. Text('TextAlign set to Center.')
  11. .textAlign(TextAlign.Center)
  12. .fontSize(12)
  13. .border({ width: 1 })
  14. .padding(10)
  15. .width('100%')
  16. Text('TextAlign set to Start.')
  17. .textAlign(TextAlign.Start)
  18. .fontSize(12)
  19. .border({ width: 1 })
  20. .padding(10)
  21. .width('100%')
  22. Text('TextAlign set to End.')
  23. .textAlign(TextAlign.End)
  24. .fontSize(12)
  25. .border({ width: 1 })
  26. .padding(10)
  27. .width('100%')
  28. // 多行文本
  29. Text('This is the text content with textAlign set to Center.')
  30. .textAlign(TextAlign.Center)
  31. .fontSize(12)
  32. .border({ width: 1 })
  33. .padding(10)
  34. .width('100%')
  35. Text('This is the text content with textAlign set to Start.')
  36. .textAlign(TextAlign.Start)
  37. .fontSize(12)
  38. .border({ width: 1 })
  39. .padding(10)
  40. .width('100%')
  41. Text('This is the text content with textAlign set to End.')
  42. .textAlign(TextAlign.End)
  43. .fontSize(12)
  44. .border({ width: 1 })
  45. .padding(10)
  46. .width('100%')
  47. // 文本超長時顯示方式
  48. Text('TextOverflow+maxLines').fontSize(9).fontColor(0xCCCCCC)
  49. // 超出maxLines截斷內(nèi)容展示
  50. 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.')
  51. .textOverflow({ overflow: TextOverflow.Clip })
  52. .maxLines(1)
  53. .fontSize(12)
  54. .border({ width: 1 })
  55. .padding(10)
  56. // 超出maxLines展示省略號
  57. Text('This is set textOverflow to Ellipsis text content This is set textOverflow to Ellipsis text content.'.split('')
  58. .join('\u200B'))
  59. .textOverflow({ overflow: TextOverflow.Ellipsis })
  60. .maxLines(1)
  61. .fontSize(12)
  62. .border({ width: 1 })
  63. .padding(10)
  64. Text('lineHeight').fontSize(9).fontColor(0xCCCCCC)
  65. Text('This is the text with the line height set. This is the text with the line height set.')
  66. .fontSize(12).border({ width: 1 }).padding(10)
  67. Text('This is the text with the line height set. This is the text with the line height set.')
  68. .fontSize(12).border({ width: 1 }).padding(10)
  69. .lineHeight(20)
  70. }.height(600).width(350).padding({ left: 35, right: 35, top: 35 })
  71. }
  72. }

示例2

decoration,baselineOffset,letterSpacing,textCase使用示例:

  1. @Entry
  2. @Component
  3. struct TextExample2 {
  4. build() {
  5. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
  6. Text('decoration').fontSize(9).fontColor(0xCCCCCC)
  7. Text('This is the text content with the decoration set to LineThrough and the color set to Red.')
  8. .decoration({
  9. type: TextDecorationType.LineThrough,
  10. color: Color.Red
  11. })
  12. .fontSize(12)
  13. .border({ width: 1 })
  14. .padding(10)
  15. .width('100%')
  16. Text('This is the text content with the decoration set to Overline and the color set to Red.')
  17. .decoration({
  18. type: TextDecorationType.Overline,
  19. color: Color.Red
  20. })
  21. .fontSize(12)
  22. .border({ width: 1 })
  23. .padding(10)
  24. .width('100%')
  25. Text('This is the text content with the decoration set to Underline and the color set to Red.')
  26. .decoration({
  27. type: TextDecorationType.Underline,
  28. color: Color.Red
  29. })
  30. .fontSize(12)
  31. .border({ width: 1 })
  32. .padding(10)
  33. .width('100%')
  34. // 文本基線偏移
  35. Text('baselineOffset').fontSize(9).fontColor(0xCCCCCC)
  36. Text('This is the text content with baselineOffset 0.')
  37. .baselineOffset(0)
  38. .fontSize(12)
  39. .border({ width: 1 })
  40. .padding(10)
  41. .width('100%')
  42. Text('This is the text content with baselineOffset 30.')
  43. .baselineOffset(30)
  44. .fontSize(12)
  45. .border({ width: 1 })
  46. .padding(10)
  47. .width('100%')
  48. Text('This is the text content with baselineOffset -20.')
  49. .baselineOffset(-20)
  50. .fontSize(12)
  51. .border({ width: 1 })
  52. .padding(10)
  53. .width('100%')
  54. // 文本字符間距
  55. Text('letterSpacing').fontSize(9).fontColor(0xCCCCCC)
  56. Text('This is the text content with letterSpacing 0.')
  57. .letterSpacing(0)
  58. .fontSize(12)
  59. .border({ width: 1 })
  60. .padding(10)
  61. .width('100%')
  62. Text('This is the text content with letterSpacing 3.')
  63. .letterSpacing(3)
  64. .fontSize(12)
  65. .border({ width: 1 })
  66. .padding(10)
  67. .width('100%')
  68. Text('This is the text content with letterSpacing -1.')
  69. .letterSpacing(-1)
  70. .fontSize(12)
  71. .border({ width: 1 })
  72. .padding(10)
  73. .width('100%')
  74. Text('textCase').fontSize(9).fontColor(0xCCCCCC)
  75. Text('This is the text content with textCase set to Normal.')
  76. .textCase(TextCase.Normal)
  77. .fontSize(12)
  78. .border({ width: 1 })
  79. .padding(10)
  80. .width('100%')
  81. // 文本全小寫展示
  82. Text('This is the text content with textCase set to LowerCase.')
  83. .textCase(TextCase.LowerCase)
  84. .fontSize(12)
  85. .border({ width: 1 })
  86. .padding(10)
  87. .width('100%')
  88. // 文本全大寫展示
  89. Text('This is the text content with textCase set to UpperCase.')
  90. .textCase(TextCase.UpperCase)
  91. .fontSize(12).border({ width: 1 }).padding(10)
  92. }.height(700).width(350).padding({ left: 35, right: 35, top: 35 })
  93. }
  94. }

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號