1# Text 2 3显示一段文本的组件。 4 5> **说明:** 6> 7> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9 10## 子组件 11 12可以包含[Span](ts-basic-components-span.md)、[ImageSpan](ts-basic-components-imagespan.md)、[SymbolSpan](ts-basic-components-symbolSpan.md)和[ContainerSpan](ts-basic-components-containerspan.md)子组件。 13 14## 接口 15 16Text(content?: string | Resource , value?: TextOptions) 17 18**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 19 20**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 21 22**系统能力:** SystemCapability.ArkUI.ArkUI.Full 23 24**参数:** 25 26| 参数名 | 类型 | 必填 | 说明 | 27| -------- | -------- | -------- | -------- | 28| content | string \| [Resource](ts-types.md#resource) | 否 | 文本内容。包含子组件Span且未设置[属性字符串](ts-universal-styled-string.md#属性字符串)时不生效,显示Span内容,并且此时text组件的样式不生效。<br/>默认值:' ' | 29| value<sup>11+</sup> | [TextOptions](#textoptions11) | 否 | 文本组件初始化选项。| 30 31## 属性 32 33除支持[通用属性](ts-universal-attributes-size.md),还支持以下属性: 34 35### textAlign 36 37textAlign(value: TextAlign) 38 39设置文本段落在水平方向的对齐方式。 40 41文本段落宽度占满Text组件宽度。 42 43可通过[align](ts-universal-attributes-location.md)属性控制文本段落在垂直方向上的位置,此组件中不可通过align属性控制文本段落在水平方向上的位置,即align属性中Alignment.TopStart、Alignment.Top、Alignment.TopEnd效果相同,控制内容在顶部,Alignment.Start、Alignment.Center、Alignment.End效果相同,控制内容垂直居中,Alignment.BottomStart、Alignment.Bottom、Alignment.BottomEnd效果相同,控制内容在底部。结合TextAlign属性可控制内容在水平方向的位置。 44 45当textAlign属性设置为TextAlign.JUSTIFY时,需要根据文本内容设置[wordBreak](#wordbreak11)属性,且最后一行文本不参与两端对齐,为水平对齐首部效果。 46 47**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 48 49**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 50 51**系统能力:** SystemCapability.ArkUI.ArkUI.Full 52 53**参数:** 54 55| 参数名 | 类型 | 必填 | 说明 | 56| ------ | ------------------------------------------- | ---- | ---------------------------------------------------------- | 57| value | [TextAlign](ts-appendix-enums.md#textalign) | 是 | 文本段落在水平方向的对齐方式。<br/>默认值:TextAlign.Start | 58 59### textOverflow 60 61textOverflow(value: { overflow: TextOverflow }) 62 63设置文本超长时的显示方式。 64 65文本截断是按字截断。例如,英文以单词为最小单位进行截断,若需要以字母为单位进行截断,可在字母间添加零宽空格:\u200B。从API version 11开始,建议优先组合wordBreak属性设置为WordBreak.BREAK_ALL方式实现字母为单位进行截断,使用[示例4](#示例4设置文本断行及折行)。 66 67当overflow设置为TextOverflow.None、TextOverflow.Clip、TextOverflow.Ellipsis时,需配合maxLines使用,单独设置不生效。设置TextOverflow.None与TextOverflow.Clip效果一样。 68 69当overflow设置为TextOverflow.MARQUEE时,文本在一行内滚动显示,设置maxLines及copyOption属性均不生效。在文本不可滚动时,设置textAlign属性生效;在文本可滚动时,设置textAlign属性不生效。在跑马灯模式下,Text组件clip属性默认为true。属性字符串的[CustomSpan](ts-universal-styled-string.md#customspan)不支持跑马灯模式。 70 71从API version 12开始,当overflow设置为TextOverflow.MARQUEE时,支持ImageSpan组件,文本和图片在一行内滚动显示。 72 73**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 74 75**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 76 77**系统能力:** SystemCapability.ArkUI.ArkUI.Full 78 79**参数:** 80 81| 参数名 | 类型 | 必填 | 说明 | 82| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 83| value | {overflow: [TextOverflow](ts-appendix-enums.md#textoverflow)} | 是 | 文本超长时的显示方式。<br/>默认值:{overflow: TextOverflow.Clip} | 84 85### maxLines 86 87maxLines(value: number) 88 89设置文本的最大行数。默认情况下,文本是自动折行的,如果指定此属性,则文本最多不会超过指定的行。如果有多余的文本,可以通过[textOverflow](#textoverflow)来指定截断方式。 90 91**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 92 93**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 94 95**系统能力:** SystemCapability.ArkUI.ArkUI.Full 96 97**参数:** 98 99| 参数名 | 类型 | 必填 | 说明 | 100| ------ | ------ | ---- | ---------------- | 101| value | number | 是 | 文本的最大行数。 | 102 103### lineHeight 104 105lineHeight(value: number | string | Resource) 106 107设置文本的文本行高,设置值不大于0时,不限制文本行高,自适应字体大小,number类型时单位为fp。 108 109**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 110 111**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 112 113**系统能力:** SystemCapability.ArkUI.ArkUI.Full 114 115**参数:** 116 117| 参数名 | 类型 | 必填 | 说明 | 118| ------ | ------------------------------------------------------------ | ---- | ---------------- | 119| value | number \| string \| [Resource](ts-types.md#resource) | 是 | 文本的文本行高。 | 120 121### decoration 122 123decoration(value: DecorationStyleInterface) 124 125设置文本装饰线样式及其颜色。 126 127**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 128 129**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 130 131**系统能力:** SystemCapability.ArkUI.ArkUI.Full 132 133**参数:** 134 135| 参数名 | 类型 | 必填 | 说明 | 136| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 137| value | [DecorationStyleInterface<sup>12+</sup>](ts-universal-styled-string.md#decorationstyleinterface对象说明) | 是 | 文本装饰线样式对象。<br/>默认值:<br/>{<br/> type: TextDecorationType.None,<br/> color: Color.Black,<br/> style: TextDecorationStyle.SOLID <br/>}<br/>**说明:** <br/>style参数不支持卡片能力。 | 138 139### baselineOffset 140 141baselineOffset(value: number | string) 142 143设置文本基线的偏移量,设置该值为百分比时,按默认值显示。 144 145正数内容向上偏移,负数向下偏移。 146 147**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 148 149**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 150 151**系统能力:** SystemCapability.ArkUI.ArkUI.Full 152 153**参数:** 154 155| 参数名 | 类型 | 必填 | 说明 | 156| ------ | -------------------------- | ---- | -------------------------------- | 157| value | number \| string | 是 | 文本基线的偏移量。<br/>默认值:0 | 158 159### letterSpacing 160 161letterSpacing(value: number | string) 162 163设置文本字符间距。设置该值为百分比时,按默认值显示。设置该值为0时,按默认值显示。 164 165当取值为负值时,文字会发生压缩,负值过小时会将组件内容区大小压缩为0,导致无内容显示。 166 167**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 168 169**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 170 171**系统能力:** SystemCapability.ArkUI.ArkUI.Full 172 173**参数:** 174 175| 参数名 | 类型 | 必填 | 说明 | 176| ------ | -------------------------- | ---- | -------------- | 177| value | number \| string | 是 | 文本字符间距。 | 178 179### minFontSize 180 181minFontSize(value: number | string | Resource) 182 183设置文本最小显示字号。 184 185需配合[maxFontSize](#maxfontsize)以及[maxLines](#maxlines)或布局大小限制使用,单独设置不生效,对子组件和属性字符串不生效。 186 187自适应字号生效时,fontSize设置不生效。 188 189minFontSize小于或等于0时,自适应字号不生效。 190 191**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 192 193**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 194 195**系统能力:** SystemCapability.ArkUI.ArkUI.Full 196 197**参数:** 198 199| 参数名 | 类型 | 必填 | 说明 | 200| ------ | ------------------------------------------------------------ | ---- | ------------------ | 201| value | number \| string \| [Resource](ts-types.md#resource) | 是 | 文本最小显示字号。 | 202 203### maxFontSize 204 205maxFontSize(value: number | string | Resource) 206 207设置文本最大显示字号。 208 209需配合[minFontSize](#minfontsize)以及[maxLines](#maxlines)或布局大小限制使用,单独设置不生效,对子组件和属性字符串不生效。 210 211自适应字号生效时,fontSize设置不生效。 212 213**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 214 215**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 216 217**系统能力:** SystemCapability.ArkUI.ArkUI.Full 218 219**参数:** 220 221| 参数名 | 类型 | 必填 | 说明 | 222| ------ | ------------------------------------------------------------ | ---- | ------------------ | 223| value | number \| string \| [Resource](ts-types.md#resource) | 是 | 文本最大显示字号。 | 224 225### textCase 226 227textCase(value: TextCase) 228 229设置文本大小写。 230 231**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 232 233**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 234 235**系统能力:** SystemCapability.ArkUI.ArkUI.Full 236 237**参数:** 238 239| 参数名 | 类型 | 必填 | 说明 | 240| ------ | ----------------------------------------- | ---- | ----------------------------------------- | 241| value | [TextCase](ts-appendix-enums.md#textcase) | 是 | 文本大小写。<br />默认值:TextCase.Normal | 242 243### fontColor 244 245fontColor(value: ResourceColor) 246 247设置字体颜色。 248 249**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 250 251**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 252 253**系统能力:** SystemCapability.ArkUI.ArkUI.Full 254 255**参数:** 256 257| 参数名 | 类型 | 必填 | 说明 | 258| ------ | ------------------------------------------ | ---- | ---------- | 259| value | [ResourceColor](ts-types.md#resourcecolor) | 是 | 字体颜色。 | 260 261### fontSize 262 263fontSize(value: number | string | Resource) 264 265设置字体大小。 266 267**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 268 269**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 270 271**系统能力:** SystemCapability.ArkUI.ArkUI.Full 272 273**参数:** 274 275| 参数名 | 类型 | 必填 | 说明 | 276| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 277| value | number \| string \| [Resource](ts-types.md#resource) | 是 | 字体大小。fontSize为number类型时,使用fp单位。字体默认大小16fp。不支持设置百分比字符串。 | 278 279### fontStyle 280 281fontStyle(value: FontStyle) 282 283设置字体样式。 284 285**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 286 287**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 288 289**系统能力:** SystemCapability.ArkUI.ArkUI.Full 290 291**参数:** 292 293| 参数名 | 类型 | 必填 | 说明 | 294| ------ | ------------------------------------------- | ---- | --------------------------------------- | 295| value | [FontStyle](ts-appendix-enums.md#fontstyle) | 是 | 字体样式。<br/>默认值:FontStyle.Normal | 296 297### fontWeight 298 299fontWeight(value: number | FontWeight | string) 300 301设置文本的字体粗细,设置过大可能会在不同字体下有截断。 302 303**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 304 305**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 306 307**系统能力:** SystemCapability.ArkUI.ArkUI.Full 308 309**参数:** 310 311| 参数名 | 类型 | 必填 | 说明 | 312| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 313| value | number \| [FontWeight](ts-appendix-enums.md#fontweight) \| string | 是 | 文本的字体粗细,number类型取值[100, 900],取值间隔为100,默认为400,取值越大,字体越粗。string类型仅支持number类型取值的字符串形式,例如"400",以及"bold"、"bolder"、"lighter"、"regular"、"medium",分别对应FontWeight中相应的枚举值。<br/>默认值:FontWeight.Normal | 314 315### fontFamily 316 317fontFamily(value: string | Resource) 318 319设置字体列表。 320 321**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 322 323**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 324 325**系统能力:** SystemCapability.ArkUI.ArkUI.Full 326 327**参数:** 328 329| 参数名 | 类型 | 必填 | 说明 | 330| ------ | ---------------------------------------------------- | ---- | ------------------------------------------------------------ | 331| value | string \| [Resource](ts-types.md#resource) | 是 | 字体列表。默认字体'HarmonyOS Sans'。<br>应用当前支持'HarmonyOS Sans'字体和[注册自定义字体](../js-apis-font.md)。<br>卡片当前仅支持'HarmonyOS Sans'字体。 | 332 333### copyOption<sup>9+</sup> 334 335copyOption(value: CopyOptions) 336 337设置组件是否支持文本可复制粘贴。设置copyOptions为CopyOptions.InApp或者CopyOptions.LocalDevice,长按文本,会弹出文本选择菜单,可选中文本并进行复制、全选操作。 338 339由于卡片没有长按事件,此场景下长按文本,不会弹出文本选择菜单。 340 341**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 342 343**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 344 345**系统能力:** SystemCapability.ArkUI.ArkUI.Full 346 347**参数:** 348 349| 参数名 | 类型 | 必填 | 说明 | 350| ------ | ------------------------------------------------ | ---- | ---------------------------------------------------------- | 351| value | [CopyOptions](ts-appendix-enums.md#copyoptions9) | 是 | 组件是否支持文本可复制粘贴。<br />默认值:CopyOptions.None | 352 353### draggable<sup>9+</sup> 354 355draggable(value: boolean) 356 357设置选中文本拖拽效果。 358 359不能和[onDragStart](ts-universal-events-drag-drop.md)事件同时使用。 360 361需配合[CopyOptions](ts-appendix-enums.md#copyoptions9)一起使用,设置copyOptions为CopyOptions.InApp或者CopyOptions.LocalDevice,并且draggable设置为true时,支持对选中文本的拖拽以及选中内容复制到输入框。 362 363**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 364 365**系统能力:** SystemCapability.ArkUI.ArkUI.Full 366 367**参数:** 368 369| 参数名 | 类型 | 必填 | 说明 | 370| ------ | ------- | ---- | ------------------------------------ | 371| value | boolean | 是 | 选中文本拖拽效果。<br/>默认值:false | 372 373### font<sup>10+</sup> 374 375font(value: Font) 376 377设置文本样式。包括字体大小、字体粗细、字体族和字体风格。 378 379**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 380 381**系统能力:** SystemCapability.ArkUI.ArkUI.Full 382 383**参数:** 384 385| 参数名 | 类型 | 必填 | 说明 | 386| ------ | ------- | ---- | ---------- | 387| value | [Font](ts-types.md#font) | 是 | 文本样式。 | 388 389### textShadow<sup>10+</sup> 390 391textShadow(value: ShadowOptions | Array<ShadowOptions>) 392 393设置文字阴影效果。 394 395不支持fill字段,不支持智能取色模式。 396 397从API version 11开始,该接口支持以数组形式入参,实现多重文字阴影。 398 399**卡片能力:** 从API version 10开始,该接口支持在ArkTS卡片中使用。 400 401**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 402 403**系统能力:** SystemCapability.ArkUI.ArkUI.Full 404 405**参数:** 406 407| 参数名 | 类型 | 必填 | 说明 | 408| ------ | ------------------------------------------------------------ | ---- | -------------- | 409| value | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions对象说明) \| Array<[ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions对象说明)><sup>11+</sup> | 是 | 文字阴影效果。 | 410 411### heightAdaptivePolicy<sup>10+</sup> 412 413heightAdaptivePolicy(value: TextHeightAdaptivePolicy) 414 415设置文本自适应高度的方式。 416 417当设置为TextHeightAdaptivePolicy.MAX_LINES_FIRST时,优先使用[maxLines](#maxlines)属性来调整文本高度。如果使用maxLines属性的布局大小超过了布局约束,则尝试在[minFontSize](#minfontsize)和[maxFontSize](#maxfontsize)的范围内缩小字体以显示更多文本。 418 419当设置为TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST时,优先使用minFontSize属性来调整文本高度。如果使用minFontSize属性可以将文本布局在一行中,则尝试在minFontSize和maxFontSize的范围内增大字体并使用最大可能的字体大小。 420 421当设置为TextHeightAdaptivePolicy.LAYOUT_CONSTRAINT_FIRST时,优先使用布局约束来调整文本高度。如果布局大小超过布局约束,则尝试在minFontSize和maxFontSize的范围内缩小字体以满足布局约束。如果将字体大小缩小到minFontSize后,布局大小仍然超过布局约束,则删除超过布局约束的行。 422 423**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 424 425**系统能力:** SystemCapability.ArkUI.ArkUI.Full 426 427**参数:** 428 429| 参数名 | 类型 | 必填 | 说明 | 430| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 431| value | [TextHeightAdaptivePolicy](ts-appendix-enums.md#textheightadaptivepolicy10) | 是 | 文本自适应高度的方式。<br/>默认值:TextHeightAdaptivePolicy.MAX_LINES_FIRST | 432 433### textIndent<sup>10+</sup> 434 435textIndent(value: Length) 436 437设置首行文本缩进。 438 439**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 440 441**系统能力:** SystemCapability.ArkUI.ArkUI.Full 442 443**参数:** 444 445| 参数名 | 类型 | 必填 | 说明 | 446| ------ | ---------------------------- | ---- | ---------------------------- | 447| value | [Length](ts-types.md#length) | 是 | 首行文本缩进。<br/>默认值:0 | 448 449### wordBreak<sup>11+</sup> 450 451wordBreak(value: WordBreak) 452 453设置断行规则。WordBreak.BREAK_ALL与{overflow: TextOverflow.Ellipsis},maxLines组合使用可实现英文单词按字母截断,超出部分以省略号显示 454 455**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 456 457**系统能力:** SystemCapability.ArkUI.ArkUI.Full 458 459**参数:** 460 461| 参数名 | 类型 | 必填 | 说明 | 462| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 463| value | [WordBreak](ts-appendix-enums.md#wordbreak11) | 是 | 断行规则。 <br />默认值:WordBreak.BREAK_WORD | 464 465### selection<sup>11+</sup> 466 467selection(selectionStart: number, selectionEnd: number) 468 469设置选中区域。选中区域高亮且显示手柄和文本选择菜单。 470 471当copyOption设置为CopyOptions.None时,设置selection属性不生效。 472 473当overflow设置为TextOverflow.MARQUEE时,设置selection属性不生效。 474 475当selectionStart大于等于selectionEnd时不选中。可选范围为[0, textSize],textSize为文本内容最大字符数,入参小于0处理为0,大于textSize处理为textSize。 476 477当selectionStart或selectionEnd在截断不可见区域时不选中。截断为false时超出父组件的文本选中区域生效。 478 479**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 480 481**系统能力:** SystemCapability.ArkUI.ArkUI.Full 482 483**参数:** 484 485| 参数名 | 类型 | 必填 | 说明 | 486| -------------- | ------ | ---- | ------------------------------------ | 487| selectionStart | number | 是 | 所选文本的起始位置。<br />默认值:-1 | 488| selectionEnd | number | 是 | 所选文本的结束位置。<br />默认值:-1 | 489 490### ellipsisMode<sup>11+</sup> 491 492ellipsisMode(value: EllipsisMode) 493 494设置省略位置。ellipsisMode属性需要配合overflow设置为TextOverflow.Ellipsis以及maxLines使用,单独设置ellipsisMode属性不生效。 495 496EllipsisMode.START和EllipsisMode.CENTER仅在单行超长文本生效。 497 498**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 499 500**系统能力:** SystemCapability.ArkUI.ArkUI.Full 501 502**参数:** 503 504| 参数名 | 类型 | 必填 | 说明 | 505| ------ | --------------------------------------------------- | ---- | ----------------------------------------- | 506| value | [EllipsisMode](ts-appendix-enums.md#ellipsismode11) | 是 | 省略位置。 <br />默认值:EllipsisMode.END | 507 508### enableDataDetector<sup>11+</sup> 509 510enableDataDetector(enable: boolean) 511 512设置是否进行文本特殊实体识别。 513 514该接口依赖设备底层应具有文本识别能力,否则设置不会生效。 515 516当enableDataDetector设置为true,同时不设置dataDetectorConfig属性时,默认识别所有类型的实体,所识别实体的color和decoration会被更改为如下样式: 517 518```ts 519color: '#ff007dff' 520decoration:{ 521 type: TextDecorationType.Underline, 522 color: '#ff007dff', 523 style: TextDecorationStyle.SOLID 524} 525``` 526 527触摸点击和鼠标右键点击实体,会根据实体类型弹出对应的实体操作菜单,鼠标左键点击实体会直接响应菜单的第一个选项。 528 529当overflow设置为TextOverflow.MARQUEE时,该功能不会生效。 530 531当copyOption设置为CopyOptions.None时,点击实体弹出的菜单没有选择文本和复制功能。当copyOption不为CopyOptions.None,且textSelectable设置为TextSelectableMode.UNSELECTABLE时,仍然具有实体复制功能,但没有选择文本功能。 532 533**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 534 535**系统能力:** SystemCapability.ArkUI.ArkUI.Full 536 537**参数:** 538 539| 参数名 | 类型 | 必填 | 说明 | 540| ------ | ------- | ---- | --------------------------------- | 541| enable | boolean | 是 | 使能文本识别。<br/>默认值: false | 542 543### dataDetectorConfig<sup>11+</sup> 544 545dataDetectorConfig(config: TextDataDetectorConfig) 546 547设置文本识别配置。 548 549需配合[enableDataDetector](#enabledatadetector11)一起使用,设置enableDataDetector为true时,dataDetectorConfig的配置才能生效。 550 551当有两个实体A、B重叠时,按以下规则保留实体: 552 5531. 若A ⊂ B,则保留B,反之则保留A。 554 5552. 当A ⊄ B且B ⊄ A时,若A.start < B.start,则保留A,反之则保留B。 556 557**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 558 559**系统能力:** SystemCapability.ArkUI.ArkUI.Full 560 561**参数:** 562 563| 参数名 | 类型 | 必填 | 说明 | 564| ------ | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 565| config | [TextDataDetectorConfig](ts-text-common.md#textdatadetectorconfig11对象说明) | 是 | 文本识别配置。| 566 567### bindSelectionMenu<sup>11+</sup> 568 569bindSelectionMenu(spanType: TextSpanType, content: CustomBuilder, responseType: TextResponseType, 570 options?: SelectionMenuOptions) 571 572设置自定义选择菜单。 573 574bindSelectionMenu长按响应时长为600ms,bindContextMenu长按响应时长为800ms,同时绑定且触发方式均为长按时,优先响应bindSelectionMenu。 575 576自定义菜单超长时,建议内部嵌套[Scroll](./ts-container-scroll.md)组件使用,避免键盘被遮挡。 577 578**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 579 580**系统能力:** SystemCapability.ArkUI.ArkUI.Full 581 582**参数:** 583 584| 参数名 | 类型 | 必填 | 说明 | 585| ------------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 586| spanType | [TextSpanType](#textspantype11枚举说明) | 是 | 选择菜单的类型。<br/>默认值:TextSpanType.TEXT | 587| content | [CustomBuilder](ts-types.md#custombuilder8) | 是 | 选择菜单的内容。 | 588| responseType | [TextResponseType](#textresponsetype11枚举说明) | 是 | 选择菜单的响应类型。<br/>默认值:TextResponseType.LONG_PRESS | 589| options | [SelectionMenuOptions](ts-basic-components-richeditor.md#selectionmenuoptions10) | 否 | 选择菜单的选项。 | 590 591### fontFeature<sup>12+</sup> 592 593fontFeature(value: string) 594 595设置文字特性效果,比如数字等宽的特性。 596 597格式为:normal \| \<feature-tag-value\> 598 599\<feature-tag-value\>的格式为:\<string\> \[ \<integer\> \| on \| off ] 600 601\<feature-tag-value\>的个数可以有多个,中间用','隔开。 602 603例如,使用等宽数字的输入格式为:"ss01" on。 604 605**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。 606 607**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 608 609**系统能力:** SystemCapability.ArkUI.ArkUI.Full 610 611**参数:** 612 613| 参数名 | 类型 | 必填 | 说明 | 614| ------ | ------ | ---- | -------------- | 615| value | string | 是 | 文字特性效果。 | 616 617fontFeature属性列表<br/> 618 619 620设置 Font Feature 属性,Font Feature 是 OpenType 字体的高级排版能力,如支持连字、数字等宽等特性,一般用在自定义字体中,其能力需要字体本身支持。 621更多 Font Feature 能力介绍可参考 https://www.w3.org/TR/css-fonts-3/#font-feature-settings-prop 和 https://sparanoid.com/lab/opentype-features/ 622 623> **说明:** 624> 625> 不支持Text内同时存在文本内容和Span或ImageSpan子组件。如果同时存在,只显示Span或ImageSpan内的内容。 626> 627> 字体排版引擎会对开发者传入的宽度[width](ts-universal-attributes-size.md#width)进行向下取整,保证是整型像素后进行排版。如果字体排版引擎向上取整,可能会出现文字右侧被截断。 628> 629> 当多个Text组件在[Row](ts-container-row.md)容器内布局且没有设置具体的布局分配信息时,Text会以Row的最大尺寸进行布局。如果需要子组件主轴累加的尺寸不超过Row容器主轴的尺寸,可以设置[layoutWeight](ts-universal-attributes-size.md#layoutweight)或者是以[Flex](ts-universal-attributes-flex-layout.md)布局来约束子组件的主轴尺寸。 630 631### lineSpacing<sup>12+</sup> 632 633lineSpacing(value: LengthMetrics) 634 635设置文本的行间距,设置值不大于0时,取默认值0。 636 637**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 638 639**系统能力:** SystemCapability.ArkUI.ArkUI.Full 640 641**参数:** 642 643| 参数名 | 类型 | 必填 | 说明 | 644| ------ | ------------------------------------------------------------ | ---- | ---------------- | 645| value | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | 是 | 文本的行间距。默认值:0 | 646 647### privacySensitive<sup>12+</sup> 648 649privacySensitive(supported: boolean) 650 651设置是否支持卡片敏感隐私信息。 652 653**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。 654 655**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 656 657**系统能力:** SystemCapability.ArkUI.ArkUI.Full 658 659**参数:** 660 661| 参数名 | 类型 | 必填 | 说明 | 662| --------- | ------- | ---- | ------------------------------------------------------------ | 663| supported | boolean | 是 | 是否支持卡片敏感隐私信息。<br/>默认值为false,当设置为true时,隐私模式下文字将被遮罩为横杠“-”样式。<br/>**说明:** <br/>设置null则不敏感。<br/>进入隐私模式需要[卡片框架支持](./ts-universal-attributes-obscured.md)。 | 664 665### lineBreakStrategy<sup>12+</sup> 666 667lineBreakStrategy(strategy: LineBreakStrategy) 668 669设置折行规则。该属性在wordBreak不等于breakAll的时候生效,不支持连词符。 670 671**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 672 673**系统能力:** SystemCapability.ArkUI.ArkUI.Full 674 675**参数:** 676 677| 参数名 | 类型 | 必填 | 说明 | 678| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------- | 679| strategy | [LineBreakStrategy](ts-appendix-enums.md#linebreakstrategy12) | 是 | 文本的折行规则。 <br />默认值:LineBreakStrategy.GREEDY | 680 681### textSelectable<sup>12+</sup> 682 683textSelectable(mode: TextSelectableMode) 684 685设置是否支持文本可选择、可获焦以及Touch后能否获取焦点。 686 687**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 688 689**系统能力:** SystemCapability.ArkUI.ArkUI.Full 690 691**参数:** 692 693| 参数名 | 类型 | 必填 | 说明 | 694| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 695| mode | [TextSelectableMode](ts-appendix-enums.md#textselectablemode12) | 是 | 文本是否支持可选择、可获焦。 <br />默认值:TextSelectableMode.SELECTABLE_UNFOCUSABLE | 696 697### editMenuOptions<sup>12+</sup> 698 699editMenuOptions(editMenu: EditMenuOptions) 700 701设置自定义菜单扩展项,允许用户设置扩展项的文本内容、图标、回调方法。 702 703**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 704 705**系统能力:** SystemCapability.ArkUI.ArkUI.Full 706 707**参数:** 708 709| 参数名 | 类型 | 必填 | 说明 | 710| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 711| editMenu | [EditMenuOptions](ts-text-common.md#editmenuoptions) | 是 | 扩展菜单选项。 | 712 713### minFontScale<sup>12+</sup> 714 715minFontScale(scale: number | Resource) 716 717设置文本最小的字体缩放倍数。 718 719**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 720 721**系统能力:** SystemCapability.ArkUI.ArkUI.Full 722 723**参数:** 724 725| 参数名 | 类型 | 必填 | 说明 | 726| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 727| scale | number \| [Resource](ts-types.md#resource) | 是 | 文本最小的字体缩放倍数。<br/>取值范围:[0, 1]<br/>**说明:** <br/>设置的值小于0时,按值为0处理,设置的值大于1,按值为1处理,异常值默认不生效。 | 728 729### maxFontScale<sup>12+</sup> 730 731maxFontScale(scale: number | Resource) 732 733设置文本最大的字体缩放倍数。 734 735**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 736 737**系统能力:** SystemCapability.ArkUI.ArkUI.Full 738 739**参数:** 740 741| 参数名 | 类型 | 必填 | 说明 | 742| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 743| scale | number \| [Resource](ts-types.md#resource) | 是 | 文本最大的字体缩放倍数。<br/>取值范围:[1, +∞)<br/>**说明:** <br/>设置的值小于1时,按值为1处理,异常值默认不生效。 | 744 745### halfLeading<sup>12+</sup> 746 747halfLeading(halfLeading: boolean) 748 749设置文本是否将行间距平分至行的顶部与底部。 750 751组件侧设置halfLeading优先级高于module.json5配置文件中的halfLeading配置项。 752 753**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 754 755**系统能力:** SystemCapability.ArkUI.ArkUI.Full 756 757**参数:** 758 759| 参数名 | 类型 | 必填 | 说明 | 760| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 761| halfLeading | boolean | 是 | 文本是否将行间距平分至行的顶部与底部。<br/>true表示将行间距平分至行的顶部与底部,false则不平分。<br/>默认值:false | 762 763### font<sup>12+</sup> 764 765font(fontValue: Font, options?: FontSettingOptions) 766 767设置文本样式,支持设置字体配置项。 768 769仅Text组件生效,其子组件不生效。 770 771**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。 772 773**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 774 775**系统能力:** SystemCapability.ArkUI.ArkUI.Full 776 777**参数:** 778 779| 参数名 | 类型 | 必填 | 说明 | 780| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 781| fontValue | [Font](ts-types.md#font) | 是 | 设置文本样式。 | 782| options | [FontSettingOptions](ts-text-common.md#fontsettingoptions12对象说明) | 否 | 设置字体配置项。 | 783 784### fontWeight<sup>12+</sup> 785 786fontWeight(weight: number | FontWeight | string, options?: FontSettingOptions) 787 788设置文本字重,支持设置字体配置项。 789 790仅Text组件生效,其子组件不生效。 791 792**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。 793 794**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 795 796**系统能力:** SystemCapability.ArkUI.ArkUI.Full 797 798**参数:** 799 800| 参数名 | 类型 | 必填 | 说明 | 801| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 802| weight | number \| [FontWeight](ts-appendix-enums.md#fontweight) \| string | 是 | 设置文本字重。 | 803| options | [FontSettingOptions](ts-text-common.md#fontsettingoptions12对象说明) | 否 | 设置字体配置项。 | 804 805### enableHapticFeedback<sup>13+</sup> 806 807enableHapticFeedback(isEnabled: boolean) 808 809设置是否开启触控反馈。 810 811**原子化服务API:** 从API version 13开始,该接口支持在原子化服务中使用。 812 813**系统能力:** SystemCapability.ArkUI.ArkUI.Full 814 815**参数:** 816 817| 参数名 | 类型 | 必填 | 说明 | 818| ------ | ------- | ---- | ---------------------------------- | 819| isEnabled | boolean | 是 | 是否开启触控反馈。<br/>默认值:true | 820 821> **说明:** 822> 823> 开启触控反馈时,需要在工程的module.json5中配置requestPermissions字段开启振动权限,配置如下: 824> ```json 825> "requestPermissions": [ 826> { 827> "name": "ohos.permission.VIBRATE", 828> } 829> ] 830> ``` 831 832### caretColor<sup>14+</sup> 833 834caretColor(color: ResourceColor) 835 836设置文本框选中区域手柄颜色。 837 838**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 839 840**系统能力:** SystemCapability.ArkUI.ArkUI.Full 841 842**参数:** 843 844| 参数名 | 类型 | 必填 | 说明 | 845| ------ | ------------------------------------------ | ---- | -------------------------------------- | 846| color | [ResourceColor](ts-types.md#resourcecolor) | 是 | 文本选中手柄颜色。<br/>默认值:'#007DFF' | 847 848### selectedBackgroundColor<sup>14+</sup> 849 850selectedBackgroundColor(color: ResourceColor) 851 852设置文本选中底板颜色。如果未设置不透明度,默认为20%不透明度。 853 854**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 855 856**系统能力:** SystemCapability.ArkUI.ArkUI.Full 857 858**参数:** 859 860| 参数名 | 类型 | 必填 | 说明 | 861| ------ | ------------------------------------------ | ---- | ------------------------------------------ | 862| color | [ResourceColor](ts-types.md#resourcecolor) | 是 | 文本选中底板颜色。<br/>默认为20%不透明度。<br/>默认值:'#007DFF' | 863 864## TextSpanType<sup>11+</sup>枚举说明 865 866[Span](ts-basic-components-span.md)类型信息。 867 868**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 869 870**系统能力:** SystemCapability.ArkUI.ArkUI.Full 871 872| 名称 | 说明 | 873| -------- | -------- | 874| TEXT | Span为文字类型。 | 875| IMAGE | Span为图像类型。| 876| MIXED | Span为图文混合类型。| 877 878## TextResponseType<sup>11+</sup>枚举说明 879 880**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 881 882**系统能力:** SystemCapability.ArkUI.ArkUI.Full 883 884| 名称 | 说明 | 885| ---------- | ------------- | 886| RIGHT_CLICK | 通过鼠标右键触发菜单弹出。 | 887| LONG_PRESS | 通过长按触发菜单弹出。 | 888| SELECT | 通过鼠标选中触发菜单弹出。 | 889 890## 事件 891 892除支持[通用事件](ts-universal-events-click.md)外,还支持以下事件: 893 894### onCopy<sup>11+</sup> 895 896onCopy(callback:(value: string) => void) 897 898长按文本内部区域弹出剪贴板后,点击剪切板复制按钮,触发该回调。目前文本复制仅支持文本。 899 900**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 901 902**系统能力:** SystemCapability.ArkUI.ArkUI.Full 903 904**参数:** 905 906| 参数名 | 类型 | 必填 | 说明 | 907| ------ | ------ | ---- | ---------------- | 908| value | string | 是 | 复制的文本内容。 | 909 910### onTextSelectionChange<sup>11+</sup> 911 912onTextSelectionChange(callback: (selectionStart: number, selectionEnd: number) => void) 913 914文本选择的位置发生变化时,触发该回调。 915 916**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 917 918**系统能力:** SystemCapability.ArkUI.ArkUI.Full 919 920**参数:** 921 922| 参数名 | 类型 | 必填 | 说明 | 923| -------------- | ------ | ---- | -------------------- | 924| selectionStart | number | 是 | 所选文本的起始位置。 | 925| selectionEnd | number | 是 | 所选文本的结束位置。 | 926 927## TextOptions<sup>11+</sup> 928 929Text初始化参数。 930 931**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 932 933**系统能力:** SystemCapability.ArkUI.ArkUI.Full 934 935| 名称 | 类型 | 必填 | 说明 | 936| -------- | -------- | -------- | -------- | 937| controller | [TextController](#textcontroller11) | 是 | 文本控制器。 | 938 939## TextController<sup>11+</sup> 940 941Text组件的控制器。 942 943**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 944 945**系统能力:** SystemCapability.ArkUI.ArkUI.Full 946 947### 导入对象 948 949``` 950controller: TextController = new TextController() 951``` 952 953### closeSelectionMenu 954 955closeSelectionMenu(): void 956 957关闭自定义选择菜单或系统默认选择菜单。 958 959**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 960 961**系统能力:** SystemCapability.ArkUI.ArkUI.Full 962 963### setStyledString<sup>12+</sup> 964 965setStyledString(value: StyledString): void 966 967触发绑定或更新属性字符串。 968 969**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 970 971**系统能力:** SystemCapability.ArkUI.ArkUI.Full 972 973**参数:** 974 975| 参数名 | 类型 | 必填 | 说明 | 976| ----- | ------ | ---- | ------------------- | 977| value | [StyledString](ts-universal-styled-string.md#styledstring) | 是 | 属性字符串。<br/>**说明:** <br/>StyledString的子类[MutableStyledString](ts-universal-styled-string.md#mutablestyledstring)也可以作为入参值。 | 978 979### getLayoutManager<sup>12+</sup> 980 981getLayoutManager(): LayoutManager 982 983获取布局管理器对象。 984 985**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 986 987**系统能力:** SystemCapability.ArkUI.ArkUI.Full 988 989**返回值:** 990 991| 类型 | 说明 | 992| ---------------------------------------- | ------- | 993| [LayoutManager](ts-text-common.md#LayoutManager) | 布局管理器对象。 | 994 995## 示例 996 997### 示例1(设置文本布局) 998该示例通过textAlign、lineHeight、baselineOffset、halfLeading属性展示了文本布局的效果。 999```ts 1000// xxx.ets 1001@Extend(Text) 1002function style(TextAlign: TextAlign) { 1003 .textAlign(TextAlign) 1004 .fontSize(12) 1005 .border({ width: 1 }) 1006 .padding(10) 1007 .width('100%') 1008 .margin(5) 1009} 1010 1011@Entry 1012@Component 1013struct TextExample1 { 1014 @State changeTextAlignIndex: number = 0 1015 @State changeDecorationIndex: number = 0 1016 @State TextAlign: TextAlign[] = [TextAlign.Start, TextAlign.Center, TextAlign.End] 1017 @State TextAlignStr: string[] = ['Start', 'Center', 'End'] 1018 @State TextDecorationType: TextDecorationType[] = 1019 [TextDecorationType.LineThrough, TextDecorationType.Overline, TextDecorationType.Underline] 1020 @State TextDecorationTypeStr: string[] = ['LineThrough', 'Overline', 'Underline'] 1021 @State TextDecorationStyle: TextDecorationStyle[] = 1022 [TextDecorationStyle.SOLID, TextDecorationStyle.DOTTED, TextDecorationStyle.WAVY] 1023 @State TextDecorationStyleStr: string[] = ['SOLID', 'DOTTED', 'WAVY'] 1024 1025 build() { 1026 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { 1027 // 设置文本水平方向对齐方式 1028 // 单行文本 1029 Text('textAlign').fontSize(9).fontColor(0xCCCCCC) 1030 Text(`TextAlign set to ${this.TextAlignStr[this.changeTextAlignIndex]}.`) 1031 .style(this.TextAlign[this.changeTextAlignIndex]) 1032 1033 // 多行文本 1034 Text(`This is the text content with textAlign set to ${this.TextAlignStr[this.changeTextAlignIndex]}.`) 1035 .style(this.TextAlign[this.changeTextAlignIndex]) 1036 .margin(5) 1037 1038 Row() { 1039 Button('当前TextAlign类型:' + this.TextAlignStr[this.changeTextAlignIndex]).onClick(() => { 1040 this.changeTextAlignIndex++ 1041 if (this.changeTextAlignIndex > (this.TextAlignStr.length - 1)) { 1042 this.changeTextAlignIndex = 0 1043 } 1044 }) 1045 }.justifyContent(FlexAlign.Center).width('100%') 1046 1047 // 设置文本行高 1048 Text('lineHeight').fontSize(9).fontColor(0xCCCCCC) 1049 Text('This is the text with the line height set. This is the text with the line height set.') 1050 .style(TextAlign.Start) 1051 Text('This is the text with the line height set. This is the text with the line height set.') 1052 .style(TextAlign.Start) 1053 .lineHeight(20) 1054 1055 // 设置文本基线偏移 1056 Text('baselineOffset').fontSize(9).fontColor(0xCCCCCC) 1057 Text('This is the text content with baselineOffset 0.') 1058 .baselineOffset(0) 1059 .style(TextAlign.Start) 1060 Text('This is the text content with baselineOffset 30.') 1061 .baselineOffset(30) 1062 .style(TextAlign.Start) 1063 Text('This is the text content with baselineOffset -20.') 1064 .baselineOffset(-20) 1065 .style(TextAlign.Start) 1066 1067 // 设置文本是否居中对齐 1068 Text('halfLeading').fontSize(9).fontColor(0xCCCCCC) 1069 Text("This is the text with the halfLeading set.") 1070 .lineHeight(60) 1071 .halfLeading(true) 1072 .style(TextAlign.Start) 1073 Text("This is the text without the halfLeading set.") 1074 .lineHeight(60) 1075 .halfLeading(false) 1076 .style(TextAlign.Start) 1077 }.height(600).width('100%').padding({ left: 35, right: 35, top: 35 }) 1078 } 1079} 1080``` 1081 1082 1083### 示例2(设置文本样式) 1084 1085该示例通过decoration、letterSpacing、textCase、textShadow属性展示了不同样式的文本效果。 1086 1087```ts 1088@Extend(Text) 1089function style() { 1090 .fontSize(12) 1091 .border({ width: 1 }) 1092 .padding(10) 1093 .width('100%') 1094 .margin(5) 1095} 1096 1097@Entry 1098@Component 1099struct TextExample2 { 1100 @State changeDecorationIndex: number = 0 1101 @State TextDecorationType: TextDecorationType[] = 1102 [TextDecorationType.LineThrough, TextDecorationType.Overline, TextDecorationType.Underline] 1103 @State TextDecorationTypeStr: string[] = ['LineThrough', 'Overline', 'Underline'] 1104 @State TextDecorationStyle: TextDecorationStyle[] = 1105 [TextDecorationStyle.SOLID, TextDecorationStyle.DOTTED, TextDecorationStyle.WAVY] 1106 @State TextDecorationStyleStr: string[] = ['SOLID', 'DOTTED', 'WAVY'] 1107 1108 build() { 1109 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { 1110 Text('decoration').fontSize(9).fontColor(0xCCCCCC) 1111 Text('This is the text content with the decoration set to LineThrough and the color set to Red.') 1112 .decoration({ 1113 type: this.TextDecorationType[this.changeDecorationIndex], 1114 color: Color.Red, 1115 style: this.TextDecorationStyle[this.changeDecorationIndex] 1116 }) 1117 .style() 1118 .margin(5) 1119 1120 Row() { 1121 Button('当前decoration类型:' + this.TextDecorationTypeStr[this.changeDecorationIndex] + ' & ' + 1122 this.TextDecorationStyleStr[this.changeDecorationIndex]).onClick(() => { 1123 this.changeDecorationIndex++ 1124 if (this.changeDecorationIndex > (this.TextDecorationTypeStr.length - 1)) { 1125 this.changeDecorationIndex = 0 1126 } 1127 }) 1128 }.justifyContent(FlexAlign.Center).width('100%') 1129 1130 // 文本字符间距 1131 Text('letterSpacing').fontSize(9).fontColor(0xCCCCCC) 1132 Text('This is the text content with letterSpacing 0.') 1133 .letterSpacing(0) 1134 .style() 1135 Text('This is the text content with letterSpacing 3.') 1136 .letterSpacing(3) 1137 .style() 1138 Text('This is the text content with letterSpacing -1.') 1139 .letterSpacing(-1) 1140 .style() 1141 1142 Text('textCase').fontSize(9).fontColor(0xCCCCCC) 1143 Text('This is the text content with textCase set to Normal.') 1144 .textCase(TextCase.Normal) 1145 .style() 1146 // 文本全小写展示 1147 Text('This is the text content with textCase set to LowerCase.') 1148 .textCase(TextCase.LowerCase) 1149 .style() 1150 // 文本全大写展示 1151 Text('This is the text content with textCase set to UpperCase.') 1152 .textCase(TextCase.UpperCase) 1153 .style() 1154 1155 Text('textShadow').fontSize(9).fontColor(0xCCCCCC) 1156 // 设置文字阴影效果 1157 Text('textShadow') 1158 .style() 1159 .textAlign(TextAlign.Center) 1160 .fontSize(40) 1161 .textShadow({ 1162 radius: 10, 1163 color: Color.Black, 1164 offsetX: 0, 1165 offsetY: 0 1166 }) 1167 1168 }.height(600).width('100%').padding({ left: 35, right: 35, top: 35 }) 1169 } 1170} 1171``` 1172 1173 1174### 示例3(设置文本超长省略) 1175 1176该示例通过maxLines、textOverflow、ellipsisMode属性展示了文本超长省略以及调整省略位置的效果。 1177 1178```ts 1179@Extend(Text) 1180function style() { 1181 .textAlign(TextAlign.Center) 1182 .fontSize(12) 1183 .border({ width: 1 }) 1184 .padding(10) 1185 .width('100%') 1186 .margin(5) 1187} 1188 1189@Entry 1190@Component 1191struct TextExample3 { 1192 @State text: string = 1193 'The text component is used to display a piece of textual information.Support universal attributes and universal text attributes.' 1194 @State ellipsisModeIndex: number = 0 1195 @State ellipsisMode: EllipsisMode[] = [EllipsisMode.START, EllipsisMode.CENTER, EllipsisMode.END] 1196 @State ellipsisModeStr: string[] = ['START', 'CENTER', 'END'] 1197 1198 build() { 1199 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { 1200 // 文本超长时显示方式 1201 Text('TextOverflow+maxLines').fontSize(9).fontColor(0xCCCCCC) 1202 // 超出maxLines截断内容展示 1203 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.') 1204 .textOverflow({ overflow: TextOverflow.Clip }) 1205 .maxLines(1) 1206 .style() 1207 1208 // 超出maxLines展示省略号 1209 Text('This is set textOverflow to Ellipsis text content This is set textOverflow to Ellipsis text content.') 1210 .textOverflow({ overflow: TextOverflow.Ellipsis }) 1211 .maxLines(1) 1212 .style() 1213 1214 Text('marquee').fontSize(9).fontColor(0xCCCCCC) 1215 // 设置文本超长时以跑马灯的方式展示 1216 Text('This is the text with the text overflow set marquee') 1217 .textOverflow({ overflow: TextOverflow.MARQUEE }) 1218 .style() 1219 1220 Text('ellipsisMode').fontSize(9).fontColor(0xCCCCCC) 1221 // 设置文本超长时省略号的位置 1222 Text(this.text) 1223 .textOverflow({ overflow: TextOverflow.Ellipsis }) 1224 .ellipsisMode(this.ellipsisMode[this.ellipsisModeIndex]) 1225 .maxLines(1) 1226 .style() 1227 1228 Row() { 1229 Button('更改省略号位置:' + this.ellipsisModeStr[this.ellipsisModeIndex]).onClick(() => { 1230 this.ellipsisModeIndex++ 1231 if (this.ellipsisModeIndex > (this.ellipsisModeStr.length - 1)) { 1232 this.ellipsisModeIndex = 0 1233 } 1234 }) 1235 } 1236 }.height(600).width('100%').padding({ left: 35, right: 35, top: 35 }) 1237 } 1238} 1239``` 1240 1241 1242 1243### 示例4(设置文本断行及折行) 1244 1245该示例通过wordBreak、lineBreakStrategy、clip属性展示了文本在不同断行、折行规则下的效果以及文本超长时是否截断。 1246 1247```ts 1248// xxx.ets 1249@Extend(Text) 1250function style() { 1251 .fontSize(12) 1252 .border({ width: 1 }) 1253 .padding(10) 1254 .width('100%') 1255 .margin(5) 1256} 1257 1258@Entry 1259@Component 1260struct TextExample4 { 1261 @State text: string = 1262 'The text component is used to display a piece of textual information.Support universal attributes and universal text attributes.' 1263 @State text2: string = 1264 "They can be classified as built-in components–those directly provided by the ArkUI framework and custom components – those defined by developers" + 1265 "The built-in components include buttons radio buttons progress indicators and text You can set the rendering effect of these components in method chaining mode," + 1266 "page components are divided into independent UI units to implement independent creation development and reuse of different units on pages making pages more engineering-oriented." 1267 @State textClip: boolean = false 1268 @State wordBreakIndex: number = 0 1269 @State wordBreak: WordBreak[] = [WordBreak.NORMAL, WordBreak.BREAK_ALL, WordBreak.BREAK_WORD] 1270 @State wordBreakStr: string[] = ['NORMAL', 'BREAK_ALL', 'BREAK_WORD'] 1271 @State lineBreakStrategyIndex: number = 0 1272 @State lineBreakStrategy: LineBreakStrategy[] = 1273 [LineBreakStrategy.GREEDY, LineBreakStrategy.HIGH_QUALITY, LineBreakStrategy.BALANCED] 1274 @State lineBreakStrategyStr: string[] = ['GREEDY', 'HIGH_QUALITY', 'BALANCED'] 1275 1276 build() { 1277 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { 1278 Text('wordBreak').fontSize(9).fontColor(0xCCCCCC) 1279 // 设置文本断行规则 1280 Text(this.text) 1281 .maxLines(2) 1282 .textOverflow({ overflow: TextOverflow.Ellipsis }) 1283 .wordBreak(this.wordBreak[this.wordBreakIndex]) 1284 .style() 1285 1286 Row() { 1287 Button('当前wordBreak模式:' + this.wordBreakStr[this.wordBreakIndex]).onClick(() => { 1288 this.wordBreakIndex++ 1289 if (this.wordBreakIndex > (this.wordBreakStr.length - 1)) { 1290 this.wordBreakIndex = 0 1291 } 1292 }) 1293 } 1294 1295 Text('clip').fontSize(9).fontColor(0xCCCCCC) 1296 // 设置文本是否超长截断 1297 Text('This is set wordBreak to WordBreak text Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu.') 1298 .wordBreak(WordBreak.NORMAL) 1299 .maxLines(2) 1300 .clip(this.textClip) 1301 .style() 1302 Row() { 1303 Button('切换clip:' + this.textClip).onClick(() => { 1304 this.textClip = !this.textClip 1305 }) 1306 } 1307 1308 Text('lineBreakStrategy').fontSize(9).fontColor(0xCCCCCC) 1309 // 设置文本折行规则 1310 Text(this.text2) 1311 .lineBreakStrategy(this.lineBreakStrategy[this.lineBreakStrategyIndex]) 1312 .style() 1313 Row() { 1314 Button('当前lineBreakStrategy模式:' + this.lineBreakStrategyStr[this.lineBreakStrategyIndex]).onClick(() => { 1315 this.lineBreakStrategyIndex++ 1316 if (this.lineBreakStrategyIndex > (this.lineBreakStrategyStr.length - 1)) { 1317 this.lineBreakStrategyIndex = 0 1318 } 1319 }) 1320 } 1321 }.height(600).width('100%').padding({ left: 35, right: 35, top: 35 }) 1322 } 1323} 1324``` 1325 1326 1327 1328### 示例5(设置文本选中和复制) 1329 1330该示例通过selection、onCopy接口展示了文本选中以及触发复制回调的效果。 1331 1332```ts 1333// xxx.ets 1334@Entry 1335@Component 1336struct TextExample5 { 1337 @State onCopy: string = '' 1338 @State text: string = 'This is set selection to Selection text content This is set selection to Selection text content.' 1339 @State start: number = 0 1340 @State end: number = 20 1341 1342 build() { 1343 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.Start }) { 1344 Text(this.text) 1345 .fontSize(12) 1346 .border({ width: 1 }) 1347 .lineHeight(20) 1348 .margin(30) 1349 .copyOption(CopyOptions.InApp) 1350 .selection(this.start, this.end) 1351 .onCopy((value: string) => { 1352 this.onCopy = value 1353 }) 1354 Button('Set text selection') 1355 .margin({left:20}) 1356 .onClick(() => { 1357 // 变更文本选中起始点、终点 1358 this.start = 10 1359 this.end = 30 1360 }) 1361 Text(this.onCopy).fontSize(12).margin(10).key('copy') 1362 }.height(600).width(335).padding({ left: 35, right: 35, top: 35 }) 1363 } 1364} 1365``` 1366 1367 1368### 示例6(设置文本自适应) 1369 1370该示例通过heightAdaptivePolicy属性展示了文本自适应的效果。 1371 1372```ts 1373// xxx.ets 1374@Extend(Text) 1375function style(HeightAdaptivePolicy: TextHeightAdaptivePolicy) { 1376 .width('80%') 1377 .height(90) 1378 .borderWidth(1) 1379 .minFontSize(10) 1380 .maxFontSize(30) 1381 .maxLines(2) 1382 .margin(5) 1383 .textOverflow({ overflow: TextOverflow.Ellipsis }) 1384 .heightAdaptivePolicy(HeightAdaptivePolicy) 1385} 1386 1387@Entry 1388@Component 1389struct TextExample6 { 1390 build() { 1391 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { 1392 // 设置文本自适应高度的方式 1393 Text('heightAdaptivePolicy').fontSize(9).fontColor(0xCCCCCC) 1394 Text('This is the text with the height adaptive policy set.') 1395 .style(TextHeightAdaptivePolicy.MAX_LINES_FIRST) 1396 Text('This is the text with the height adaptive policy set.') 1397 .style(TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST) 1398 Text('This is the text with the height adaptive policy set.') 1399 .style(TextHeightAdaptivePolicy.LAYOUT_CONSTRAINT_FIRST) 1400 }.height(600).width('100%').padding({ left: 35, right: 35, top: 35 }) 1401 } 1402} 1403``` 1404 1405 1406 1407### 示例7(设置文本识别) 1408 1409该示例通过enableDataDetector、dataDetectorConfig接口实现了文本识别的功能。 1410 1411```ts 1412// xxx.ets 1413@Entry 1414@Component 1415struct TextExample7 { 1416 @State phoneNumber: string = '(86) (755) ********' 1417 @State url: string = 'www.********.com' 1418 @State email: string = '***@example.com' 1419 @State address: string = 'XX省XX市XX区XXXX' 1420 @State datetime: string = 'XX年XX月XX日XXXX' 1421 @State enableDataDetector: boolean = true 1422 @State types: TextDataDetectorType[] = [] 1423 1424 build() { 1425 Row() { 1426 Column() { 1427 Text( 1428 '电话号码:' + this.phoneNumber + '\n' + 1429 '链接:' + this.url + '\n' + 1430 '邮箱:' + this.email + '\n' + 1431 '地址:' + this.address + '\n' + 1432 '时间:' + this.datetime 1433 ) 1434 .fontSize(16) 1435 .copyOption(CopyOptions.InApp) 1436 .enableDataDetector(this.enableDataDetector) 1437 .dataDetectorConfig({types : this.types, onDetectResultUpdate: (result: string)=>{}}) 1438 .textAlign(TextAlign.Center) 1439 .borderWidth(1) 1440 .padding(10) 1441 .width('100%') 1442 } 1443 .width('100%') 1444 } 1445 .height('100%') 1446 } 1447} 1448``` 1449 1450 1451 1452### 示例8(文本绑定自定义菜单) 1453 1454该示例通过bindSelectionMenu、onTextSelectionChange、closeSelectionMenu接口实现了文本绑定自定义菜单的功能。 1455 1456```ts 1457// xxx.ets 1458@Entry 1459@Component 1460struct TextExample8 { 1461 controller: TextController = new TextController() 1462 options: TextOptions = { controller: this.controller } 1463 1464 build() { 1465 Column() { 1466 Column() { 1467 Text(undefined, this.options) { 1468 Span('Hello World') 1469 ImageSpan($r('app.media.icon')) 1470 .width('100px') 1471 .height('100px') 1472 .objectFit(ImageFit.Fill) 1473 .verticalAlign(ImageSpanAlignment.CENTER) 1474 } 1475 .copyOption(CopyOptions.InApp) 1476 .bindSelectionMenu(TextSpanType.IMAGE, this.LongPressImageCustomMenu, TextResponseType.LONG_PRESS, { 1477 onDisappear: () => { 1478 console.info(`自定义选择菜单关闭时回调`) 1479 }, 1480 onAppear: () => { 1481 console.info(`自定义选择菜单弹出时回调`) 1482 } 1483 }) 1484 .bindSelectionMenu(TextSpanType.TEXT, this.RightClickTextCustomMenu, TextResponseType.RIGHT_CLICK) 1485 .bindSelectionMenu(TextSpanType.MIXED, this.SelectMixCustomMenu, TextResponseType.SELECT) 1486 .onTextSelectionChange((selectionStart: number, selectionEnd: number) => { 1487 console.info(`文本选中区域变化回调, selectionStart: ${selectionStart}, selectionEnd: ${selectionEnd}`) 1488 }) 1489 .borderWidth(1) 1490 .borderColor(Color.Red) 1491 .width(200) 1492 .height(100) 1493 } 1494 .width('100%') 1495 .backgroundColor(Color.White) 1496 .alignItems(HorizontalAlign.Start) 1497 .padding(25) 1498 } 1499 .height('100%') 1500 } 1501 1502 @Builder 1503 RightClickTextCustomMenu() { 1504 Column() { 1505 Menu() { 1506 MenuItemGroup() { 1507 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Right Click Menu 1", labelInfo: "" }) 1508 .onClick((event) => { 1509 this.controller.closeSelectionMenu() 1510 }) 1511 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Right Click Menu 2", labelInfo: "" }) 1512 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Right Click Menu 3", labelInfo: "" }) 1513 } 1514 } 1515 .MenuStyles() 1516 } 1517 } 1518 1519 @Builder 1520 LongPressImageCustomMenu() { 1521 Column() { 1522 Menu() { 1523 MenuItemGroup() { 1524 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Long Press Image Menu 1", labelInfo: "" }) 1525 .onClick((event) => { 1526 this.controller.closeSelectionMenu() 1527 }) 1528 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Long Press Image Menu 2", labelInfo: "" }) 1529 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Long Press Image Menu 3", labelInfo: "" }) 1530 } 1531 } 1532 .MenuStyles() 1533 } 1534 } 1535 1536 @Builder 1537 SelectMixCustomMenu() { 1538 Column() { 1539 Menu() { 1540 MenuItemGroup() { 1541 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Select Mixed Menu 1", labelInfo: "" }) 1542 .onClick((event) => { 1543 this.controller.closeSelectionMenu() 1544 }) 1545 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Select Mixed Menu 2", labelInfo: "" }) 1546 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Select Mixed Menu 3", labelInfo: "" }) 1547 } 1548 } 1549 .MenuStyles() 1550 } 1551 } 1552} 1553 1554@Extend(Menu) 1555function MenuStyles() { 1556 .radius($r('sys.float.ohos_id_corner_radius_card')) 1557 .clip(true) 1558 .backgroundColor('#F0F0F0') 1559} 1560``` 1561 1562 1563 1564### 示例9(设置文本特性与行间距) 1565 1566该示例通过fontFeature、lineSpacing接口展示了设置文本特性与行间距的效果。 1567 1568```ts 1569// xxx.ets 1570import { LengthMetrics } from '@kit.ArkUI' 1571 1572@Extend(Text) 1573function style() { 1574 .fontSize(12) 1575 .border({ width: 1 }) 1576 .width('100%') 1577 // .margin(5) 1578} 1579 1580@Entry 1581@Component 1582struct TextExample9 { 1583 build() { 1584 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { 1585 Text('lineSpacing').fontSize(9).fontColor(0xCCCCCC) 1586 // 设置文本行间距 1587 Text('This is a context with no lineSpacing set.') 1588 .lineSpacing(undefined) 1589 .style() 1590 Text('This is a context with lineSpacing set to 20_px.') 1591 .lineSpacing(LengthMetrics.px(20)) 1592 .style() 1593 Text('This is the context with lineSpacing set to 20_vp.') 1594 .lineSpacing(LengthMetrics.vp(20)) 1595 .style() 1596 Text('This is the context with lineSpacing set to 20_fp.') 1597 .lineSpacing(LengthMetrics.fp(20)) 1598 .style() 1599 Text('This is the context with lineSpacing set to 20_lpx.') 1600 .lineSpacing(LengthMetrics.lpx(20)) 1601 .style() 1602 Text('This is the context with lineSpacing set to 100%.') 1603 .lineSpacing(LengthMetrics.percent(1)) 1604 .style() 1605 1606 Text('fontFeature').fontSize(9).fontColor(0xCCCCCC) 1607 // 设置文本特性 1608 Text('This is ss01 on : 0123456789') 1609 .fontFeature("\"ss01\" on") 1610 .style() 1611 Text('This is ss01 off: 0123456789') 1612 .fontFeature("\"ss01\" off") 1613 .style() 1614 }.height(300).width(350).padding({ left: 35, right: 35, top: 35 }) 1615 } 1616} 1617``` 1618 1619 1620 1621### 示例10(获取文本信息) 1622 1623该示例通过getLayoutManager接口调用文本的布局管理对象获取文本信息。 1624 1625```ts 1626// xxx.ets 1627import { text } from '@kit.ArkGraphics2D' 1628 1629@Entry 1630@Component 1631struct TextExample10 { 1632 @State lineCount: string = "" 1633 @State glyphPositionAtCoordinate: string = "" 1634 @State lineMetrics: string = "" 1635 @State rectsForRangeStr: string = "" 1636 controller: TextController = new TextController() 1637 @State textStr: string = 1638 'Hello World! 您好,世界!' 1639 1640 build() { 1641 Scroll() { 1642 Column() { 1643 Text('Text组件getLayoutManager接口获取段落相对组件的信息') 1644 .fontSize(15) 1645 .fontColor(0xCCCCCC) 1646 .width('90%') 1647 .padding(10) 1648 Text(this.textStr, { controller: this.controller }) 1649 .fontSize(25) 1650 .borderWidth(1) 1651 .onAreaChange(() => { 1652 let layoutManager: LayoutManager = this.controller.getLayoutManager() 1653 this.lineCount = "LineCount: " + layoutManager.getLineCount() 1654 }) 1655 1656 Text('LineCount').fontSize(15).fontColor(0xCCCCCC).width('90%').padding(10) 1657 Text(this.lineCount) 1658 1659 Text('GlyphPositionAtCoordinate').fontSize(15).fontColor(0xCCCCCC).width('90%').padding(10) 1660 Button("相对组件坐标[150,50]字形信息") 1661 .onClick(() => { 1662 let layoutManager: LayoutManager = this.controller.getLayoutManager() 1663 let position: PositionWithAffinity = layoutManager.getGlyphPositionAtCoordinate(150, 50) 1664 this.glyphPositionAtCoordinate = 1665 "相对组件坐标[150,50] glyphPositionAtCoordinate position: " + position.position + " affinity: " + 1666 position.affinity 1667 }) 1668 .margin({ bottom: 20, top: 10 }) 1669 Text(this.glyphPositionAtCoordinate) 1670 1671 Text('LineMetrics').fontSize(15).fontColor(0xCCCCCC).width('90%').padding(10) 1672 Button("首行行信息、文本样式信息、以及字体属性信息") 1673 .onClick(() => { 1674 let layoutManager: LayoutManager = this.controller.getLayoutManager() 1675 let lineMetrics: LineMetrics = layoutManager.getLineMetrics(0) 1676 this.lineMetrics = "lineMetrics is " + JSON.stringify(lineMetrics) + "\n\n" 1677 let runMetrics = lineMetrics.runMetrics 1678 runMetrics.forEach((value, key) => { 1679 this.lineMetrics += "runMetrics key is " + key + " " + JSON.stringify(value) + "\n\n" 1680 }) 1681 }) 1682 .margin({ bottom: 20, top: 10 }) 1683 Text(this.lineMetrics) 1684 1685 Text('getRectsForRange').fontSize(15).fontColor(0xCCCCCC).width('90%').padding(10) 1686 Button("获取指定矩形宽度和高度下,文本中任意区间范围内字符或占位符的绘制区域信息") 1687 .onClick(() => { 1688 let layoutManager: LayoutManager = this.controller.getLayoutManager() 1689 let range: TextRange = { start: 0, end: 1 } 1690 let rectsForRangeInfo: text.TextBox[] = 1691 layoutManager.getRectsForRange(range, text.RectWidthStyle.TIGHT, text.RectHeightStyle.TIGHT) 1692 this.rectsForRangeStr = "getRectsForRange result is " + "\n\n" 1693 rectsForRangeInfo.forEach((value, key) => { 1694 this.rectsForRangeStr += "rectsForRange key is " + key + " " + JSON.stringify(value) + "\n\n" 1695 }) 1696 }) 1697 .margin({ bottom: 20, top: 10 }) 1698 Text(this.rectsForRangeStr) 1699 } 1700 .margin({ top: 100, left: 8, right: 8 }) 1701 } 1702 } 1703} 1704``` 1705 1706 1707 1708### 示例11(实现键盘框选文本) 1709 1710该示例通过textSelectable属性实现了设置TextSelectMode.SELECTABLE_FOCUSABLE属性时能够触发键盘框选文本功能。 1711 1712```ts 1713// xxx.ets 1714@Entry 1715@Component 1716struct TextExample11 { 1717 @State message: string = 'TextTextTextTextTextTextTextText' + 'TextTextTextTextTextTextTextTextTextTextTextTextTextTextTextText' 1718 1719 build() { 1720 Column() { 1721 Text(this.message) 1722 .width(300) 1723 .height(100) 1724 .maxLines(5) 1725 .fontColor(Color.Black) 1726 .copyOption(CopyOptions.InApp) 1727 .selection(3, 8) 1728 .textSelectable(TextSelectableMode.SELECTABLE_FOCUSABLE) 1729 }.width('100%').margin({ top: 100 }) 1730 } 1731} 1732``` 1733 1734 1735 1736### 示例12(文本扩展自定义菜单) 1737 1738该示例通过editMenuOptions接口实现了文本设置自定义菜单扩展项的文本内容、图标以及回调的功能。 1739 1740```ts 1741// xxx.ets 1742@Entry 1743@Component 1744struct TextExample12 { 1745 @State text: string = 'Text editMenuOptions' 1746 onCreateMenu = (menuItems: Array<TextMenuItem>) => { 1747 let item1: TextMenuItem = { 1748 content: 'custom1', 1749 icon: $r('app.media.startIcon'), 1750 id: TextMenuItemId.of('custom1'), 1751 } 1752 let item2: TextMenuItem = { 1753 content: 'custom2', 1754 id: TextMenuItemId.of('custom2'), 1755 icon: $r('app.media.startIcon'), 1756 } 1757 menuItems.push(item1) 1758 menuItems.unshift(item2) 1759 return menuItems 1760 } 1761 onMenuItemClick = (menuItem: TextMenuItem, textRange: TextRange) => { 1762 if (menuItem.id.equals(TextMenuItemId.of("custom2"))) { 1763 console.log("拦截 id: custom2 start:" + textRange.start + "; end:" + textRange.end) 1764 return true 1765 } 1766 if (menuItem.id.equals(TextMenuItemId.COPY)) { 1767 console.log("拦截 COPY start:" + textRange.start + "; end:" + textRange.end) 1768 return true 1769 } 1770 if (menuItem.id.equals(TextMenuItemId.SELECT_ALL)) { 1771 console.log("不拦截 SELECT_ALL start:" + textRange.start + "; end:" + textRange.end) 1772 return false 1773 } 1774 return false 1775 } 1776 @State editMenuOptions: EditMenuOptions = { 1777 onCreateMenu: this.onCreateMenu, onMenuItemClick: this.onMenuItemClick 1778 } 1779 1780 build() { 1781 Column() { 1782 Text(this.text) 1783 .fontSize(20) 1784 .copyOption(CopyOptions.LocalDevice) 1785 .editMenuOptions(this.editMenuOptions) 1786 .margin({ top: 100 }) 1787 } 1788 .width("90%") 1789 .margin("5%") 1790 } 1791} 1792``` 1793 1794 1795 1796### 示例13(配置隐私隐藏) 1797 1798该示例通过privacySensitive属性展示了文本如何配置隐私隐藏的效果,实际显示需要卡片框架支持。 1799 1800```ts 1801// xxx.ets 1802@Entry 1803@Component 1804struct TextExample13 { 1805 build() { 1806 Column({ space: 10 }) { 1807 Text("privacySensitive") 1808 .privacySensitive(true) 1809 .margin({top :30}) 1810 } 1811 .alignItems(HorizontalAlign.Center) 1812 .width("100%") 1813 } 1814} 1815``` 1816 1817