1# TextArea 2 3多行文本输入框组件,当输入的文本内容超过组件宽度时会自动换行显示。 4 5高度未设置时,组件无默认高度,自适应内容高度。宽度未设置时,默认撑满最大宽度。 6 7> **说明:** 8> 9> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 10 11 12## 子组件 13 14无 15 16 17## 接口 18 19TextArea(value?: TextAreaOptions) 20 21**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 22 23**系统能力:** SystemCapability.ArkUI.ArkUI.Full 24 25**参数:** 26 27| 参数名 | 类型 | 必填 | 说明 | 28| ----- | ----- | ---- | ---- | 29| value | [TextAreaOptions](#textareaoptions对象说明) | 否 | TextArea组件参数。 | 30 31## TextAreaOptions对象说明 32 33**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 34 35**系统能力:** SystemCapability.ArkUI.ArkUI.Full 36 37| 名称 | 类型 | 必填 | 说明 | 38| ---- | ----- | ---- | ---- | 39| placeholder | [ResourceStr](ts-types.md#resourcestr) | 否 | 设置无输入时的提示文本。输入内容后,提示文本不显示。<br/>仅设置placeholder属性时,手柄依然跟随拖动,手柄松开后光标停留在文字开头位置。 | 40| text | [ResourceStr](ts-types.md#resourcestr) | 否 | 设置输入框当前的文本内容。</br>建议通过onChange事件将状态变量与文本实时绑定,</br>避免组件刷新时TextArea中的文本内容异常。<br />从API version 10开始,该参数支持[$$](../../../quick-start/arkts-two-way-sync.md)双向绑定变量。 | 41| controller<sup>8+</sup> | [TextAreaController](#textareacontroller8) | 否 | 设置TextArea控制器。 | 42 43 44## 属性 45 46除支持[通用属性](ts-universal-attributes-size.md),还支持以下属性: 47 48> **说明:** 49> 50> [通用属性padding](ts-universal-attributes-size.md#padding)的默认值为:<br>{<br> top: '8vp',<br> right: '16vp',<br> bottom: '8vp',<br> left: '16vp'<br> } 51> 52> 从API version 11开始,多行输入框可设置.width('auto')使组件宽度自适应文本宽度,自适应时组件宽度受constraintSize属性以及父容器传递的最大最小宽度限制,其余使用方式参考[尺寸设置](ts-universal-attributes-size.md#属性)。 53 54### placeholderColor 55 56placeholderColor(value: ResourceColor) 57 58设置placeholder文本颜色。 59 60**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 61 62**系统能力:** SystemCapability.ArkUI.ArkUI.Full 63 64**参数:** 65 66| 参数名 | 类型 | 必填 | 说明 | 67| ------ | ------------------------------------------ | ---- | -------------------------------------------- | 68| value | [ResourceColor](ts-types.md#resourcecolor) | 是 | placeholder文本颜色。<br/>默认值:跟随主题。 | 69 70### placeholderFont 71 72placeholderFont(value: Font) 73 74设置placeholder文本样式,包括字体大小,字体粗细,字体族,字体风格。当前支持'HarmonyOS Sans'字体和[注册自定义字体](../js-apis-font.md)。 75 76**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 77 78**系统能力:** SystemCapability.ArkUI.ArkUI.Full 79 80**参数:** 81 82| 参数名 | 类型 | 必填 | 说明 | 83| ------ | ------------------------ | ---- | --------------------- | 84| value | [Font](ts-types.md#font) | 是 | placeholder文本样式。 | 85 86### textAlign 87 88textAlign(value: TextAlign) 89 90设置文本在输入框中的水平对齐方式。 91 92支持TextAlign.Start、TextAlign.Center和TextAlign.End。 93 94可通过[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效果相同,控制内容在底部。 95 96当textAlign属性设置为TextAlign.JUSTIFY时,最后一行文本不参与两端对齐,为水平对齐首部效果。 97 98从API version 11开始,textAlign可设置TextAlign.JUSTIFY 99 100**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 101 102**系统能力:** SystemCapability.ArkUI.ArkUI.Full 103 104**参数:** 105 106| 参数名 | 类型 | 必填 | 说明 | 107| ------ | ------------------------------------------- | ---- | ---------------------------------------------------------- | 108| value | [TextAlign](ts-appendix-enums.md#textalign) | 是 | 文本在输入框中的水平对齐方式。<br/>默认值:TextAlign.Start | 109 110### caretColor 111 112caretColor(value: ResourceColor) 113 114设置输入框光标颜色。 115 116**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 117 118**系统能力:** SystemCapability.ArkUI.ArkUI.Full 119 120**参数:** 121 122| 参数名 | 类型 | 必填 | 说明 | 123| ------ | ------------------------------------------ | ---- | -------------------------------------- | 124| value | [ResourceColor](ts-types.md#resourcecolor) | 是 | 输入框光标颜色。<br/>默认值:'#007DFF' | 125 126> **说明:** 127> 从API version 12开始,此接口支持设置文本手柄颜色,光标和文本手柄颜色保持一致。 128 129### fontColor 130 131fontColor(value: ResourceColor) 132 133设置字体颜色。 134 135**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 136 137**系统能力:** SystemCapability.ArkUI.ArkUI.Full 138 139**参数:** 140 141| 参数名 | 类型 | 必填 | 说明 | 142| ------ | ------------------------------------------ | ---- | ---------- | 143| value | [ResourceColor](ts-types.md#resourcecolor) | 是 | 字体颜色。 | 144 145### fontSize 146 147fontSize(value: Length) 148 149设置字体大小。 150 151**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 152 153**系统能力:** SystemCapability.ArkUI.ArkUI.Full 154 155**参数:** 156 157| 参数名 | 类型 | 必填 | 说明 | 158| ------ | ---------------------------- | ---- | ------------------------------------------------------------ | 159| value | [Length](ts-types.md#length) | 是 | 字体大小。fontSize为number类型时,使用fp单位。字体默认大小16fp。不支持设置百分比字符串。 | 160 161### fontStyle 162 163fontStyle(value: FontStyle) 164 165设置字体样式。 166 167**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 168 169**系统能力:** SystemCapability.ArkUI.ArkUI.Full 170 171**参数:** 172 173| 参数名 | 类型 | 必填 | 说明 | 174| ------ | ------------------------------------------- | ---- | --------------------------------------- | 175| value | [FontStyle](ts-appendix-enums.md#fontstyle) | 是 | 字体样式。<br/>默认值:FontStyle.Normal | 176 177### fontWeight 178 179fontWeight(value: number | FontWeight | string) 180 181设置文本的字体粗细,设置过大可能会在不同字体下有截断。 182 183**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 184 185**系统能力:** SystemCapability.ArkUI.ArkUI.Full 186 187**参数:** 188 189| 参数名 | 类型 | 必填 | 说明 | 190| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 191| 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 | 192 193### fontFamily 194 195fontFamily(value: ResourceStr) 196 197设置字体列表。 198 199**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 200 201**系统能力:** SystemCapability.ArkUI.ArkUI.Full 202 203**参数:** 204 205| 参数名 | 类型 | 必填 | 说明 | 206| ------ | -------------------------------------- | ---- | ------------------------------------------------------------ | 207| value | [ResourceStr](ts-types.md#resourcestr) | 是 | 字体列表。默认字体'HarmonyOS Sans'。<br>应用当前支持'HarmonyOS Sans'字体和[注册自定义字体](../js-apis-font.md)。<br>卡片当前仅支持'HarmonyOS Sans'字体。 | 208 209### inputFilter<sup>8+</sup> 210 211inputFilter(value: ResourceStr, error?: (value: string) => void) 212 213通过正则表达式设置输入过滤器。匹配表达式的输入允许显示,不匹配的输入将被过滤。仅支持单个字符匹配,不支持字符串匹配。 214 215**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 216 217**系统能力:** SystemCapability.ArkUI.ArkUI.Full 218 219**参数:** 220 221| 参数名 | 类型 | 必填 | 说明 | 222| ------ | -------------------------------------- | ---- | ---------------------------------- | 223| value | [ResourceStr](ts-types.md#resourcestr) | 是 | 正则表达式。 | 224| error | (value: string) => void | 否 | 正则匹配失败时,返回被过滤的内容。 | 225 226### copyOption<sup>9+</sup> 227 228copyOption(value: CopyOptions) 229 230设置输入的文本是否可复制。设置CopyOptions.None时,当前TextArea中的文字无法被复制、剪切和帮写,仅支持粘贴。 231 232设置CopyOptions.None时,不允许拖拽。 233 234**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 235 236**系统能力:** SystemCapability.ArkUI.ArkUI.Full 237 238**参数:** 239 240| 参数名 | 类型 | 必填 | 说明 | 241| ------ | ------------------------------------------------ | ---- | ------------------------------------------------------------ | 242| value | [CopyOptions](ts-appendix-enums.md#copyoptions9) | 是 | 输入的文本是否可复制。<br/>默认值:CopyOptions.LocalDevice,支持设备内复制。 | 243 244### maxLength<sup>10+</sup> 245 246maxLength(value: number) 247 248设置文本的最大输入字符数。默认不设置最大输入字符数限制。到达文本最大字符限制,将无法继续输入字符,同时边框变为红色。 249 250**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 251 252**系统能力:** SystemCapability.ArkUI.ArkUI.Full 253 254**参数:** 255 256| 参数名 | 类型 | 必填 | 说明 | 257| ------ | ------ | ---- | ---------------------- | 258| value | number | 是 | 文本的最大输入字符数。 | 259 260### showCounter<sup>10+</sup> 261 262showCounter(value: boolean, options?: InputCounterOptions) 263 264设置当通过InputCounterOptions输入的字符数超过阈值时显示计数器。 265 266参数value为true时,才能设置options,文本框开启计数下标功能,需要配合maxlength(设置最大字符限制)一起使用。字符计数器显示的效果是当前输入字符数/最大可输入字符数。 267 268当输入字符数大于最大字符数乘百分比值时,显示字符计数器。如果用户设置计数器时不设置InputCounterOptions,那么当前输入字符数达到最大字符数时,边框和计数器下标将变为红色。用户同时设置参数value为true和InputCounterOptions,当thresholdPercentage数值在有效区间内,且输入字符数超过最大字符数时,边框和计数器下标将变为红色,框体抖动。highlightBorder设置为false,则不显示红色边框,计数器默认显示红色边框。内联模式下字符计数器不显示。 269 270**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 271 272**系统能力:** SystemCapability.ArkUI.ArkUI.Full 273 274**参数:** 275 276| 参数名 | 类型 | 必填 | 说明 | 277| --------------------- | ------------------------------------------------------------ | ---- | ---------------- | 278| value | boolean | 是 | 是否显示计数器。 | 279| options<sup>11+</sup> | [InputCounterOptions](ts-types.md#inputcounteroptions11对象说明) | 否 | 计数器的百分比。 | 280 281### style<sup>10+</sup> 282 283style(value: TextContentStyle) 284 285设置文本框多态样式,内联输入风格只支持TextAreaType.Normal类型。 286 287**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 288 289**系统能力:** SystemCapability.ArkUI.ArkUI.Full 290 291**参数:** 292 293| 参数名 | 类型 | 必填 | 说明 | 294| ------ | ----------------------------------------------------------- | ---- | ----------------------------------------------------- | 295| value | [TextContentStyle](ts-appendix-enums.md#textcontentstyle10) | 是 | 文本框多态样式。<br/>默认值:TextContentStyle.DEFAULT | 296 297### enableKeyboardOnFocus<sup>10+</sup> 298 299enableKeyboardOnFocus(value: boolean) 300 301设置TextArea通过点击以外的方式获焦时,是否主动拉起软键盘。 302 303从API version 10开始,获焦默认绑定输入法。 304 305**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 306 307**系统能力:** SystemCapability.ArkUI.ArkUI.Full 308 309**参数:** 310 311| 参数名 | 类型 | 必填 | 说明 | 312| ------ | ------- | ---- | ----------------------------------------------------------- | 313| value | boolean | 是 | 通过点击以外的方式获焦时,是否主动拉起软键盘。<br/>默认值:true | 314 315### selectionMenuHidden<sup>10+</sup> 316 317selectionMenuHidden(value: boolean) 318 319设置是否不弹出系统文本选择菜单。 320 321**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 322 323**系统能力:** SystemCapability.ArkUI.ArkUI.Full 324 325**参数:** 326 327| 参数名 | 类型 | 必填 | 说明 | 328| ------ | ------- | ---- | ------------------------------------------------------------ | 329| value | boolean | 是 | 是否不弹出系统文本选择菜单。<br />设置为true时,单击输入框光标、长按输入框、双击输入框、三击输入框或者右键输入框,不弹出系统文本选择菜单。<br />设置为false时,弹出系统文本选择菜单。<br />默认值:false | 330 331### barState<sup>10+</sup> 332 333barState(value: BarState) 334 335设置输入框编辑态时滚动条的显示模式。 336 337**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 338 339**系统能力:** SystemCapability.ArkUI.ArkUI.Full 340 341**参数:** 342 343| 参数名 | 类型 | 必填 | 说明 | 344| ------ | ----------------------------------------- | ---- | ------------------------------------------------------------ | 345| value | [BarState](ts-appendix-enums.md#barstate) | 是 | 输入框编辑态时滚动条的显示模式。<br/>默认值:BarState.Auto | 346 347### maxLines<sup>10+</sup> 348 349maxLines(value: number) 350 351配置textOverflow一起使用时,maxlines为可显示行数,超出截断;未配置textOverflow时,内联模式获焦状态下内容超出maxlines时,文本可滚动显示,内联模式非获焦状态下不生效maxlines,非内联模式按行截断。 352 353**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 354 355**系统能力:** SystemCapability.ArkUI.ArkUI.Full 356 357**参数:** 358 359| 参数名 | 类型 | 必填 | 说明 | 360| ------ | ----------------------------------------- | ---- | ------------------------------------------------------------ | 361| value | number | 是 | 内联输入风格编辑态时文本可显示的最大行数。<br/>默认值:3,非内联模式下,默认值为+∞,不限制最大行数。 <br/>取值范围:(0, +∞) | 362 363### customKeyboard<sup>10+</sup> 364 365customKeyboard(value: CustomBuilder, options?: KeyboardOptions) 366 367设置自定义键盘。 368 369当设置自定义键盘时,输入框激活后不会打开系统输入法,而是加载指定的自定义组件。 370 371自定义键盘的高度可以通过自定义组件根节点的height属性设置,宽度不可设置,使用系统默认值。 372 373自定义键盘采用覆盖原始界面的方式呈现,当没有开启避让模式或者输入框不需要避让的场景不会对应用原始界面产生压缩或者上提。 374 375自定义键盘无法获取焦点,但是会拦截手势事件。 376 377默认在输入控件失去焦点时,关闭自定义键盘,开发者也可以通过[TextAreaController](#textareacontroller8).[stopEditing](#stopediting10)方法控制键盘关闭。 378 379如果设备支持拍摄输入,设置自定义键盘后,该输入框会不支持拍摄输入。 380 381当设置自定义键盘时,可以通过绑定[onKeyPrelme](ts-universal-events-key.md#onkeypreime12)事件规避物理键盘的输入。 382 383**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 384 385**系统能力:** SystemCapability.ArkUI.ArkUI.Full 386 387**参数:** 388 389| 参数名 | 类型 | 必填 | 说明 | 390| --------------------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 391| value | [CustomBuilder](ts-types.md#custombuilder8) | 是 | 自定义键盘。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 392| options<sup>12+</sup> | [KeyboardOptions](ts-basic-components-richeditor.md#keyboardoptions12) | 否 | 设置自定义键盘是否支持避让功能。 | 393 394### type<sup>11+</sup> 395 396type(value: TextAreaType) 397 398设置输入框类型。 399 400**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 401 402**系统能力:** SystemCapability.ArkUI.ArkUI.Full 403 404**参数:** 405 406| 参数名 | 类型 | 必填 | 说明 | 407| ------ | --------------------------------------- | ---- | -------------------------------------------- | 408| value | [TextAreaType](#textareatype11枚举说明) | 是 | 输入框类型。<br/>默认值:TextAreaType.Normal | 409 410### enterKeyType<sup>11+</sup> 411 412enterKeyType(value: EnterKeyType) 413 414设置输入法回车键类型。 415 416**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 417 418**系统能力:** SystemCapability.ArkUI.ArkUI.Full 419 420**参数:** 421 422| 参数名 | 类型 | 必填 | 说明 | 423| ------ | ------------------------------------------------ | ---- | ---------------------------------------------------- | 424| value | [EnterKeyType](ts-basic-components-textinput.md#enterkeytype枚举说明) | 是 | 输入法回车键类型。<br/>默认值:EnterKeyType.NEW_LINE | 425 426### enableAutoFill<sup>12+</sup> 427 428enableAutoFill(value: boolean) 429 430设置是否启用自动填充。 431 432**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 433 434**系统能力:** SystemCapability.ArkUI.ArkUI.Full 435 436**参数:** 437 438| 参数名 | 类型 | 必填 | 说明 | 439| ------ | ------- | ---- | ------------------------------------------------------------ | 440| value | boolean | 是 | 是否启用自动填充。<br/>true表示启用,false表示不启用。<br/>默认值:true | 441 442### contentType<sup>12+</sup> 443 444contentType(contentType: ContentType) 445 446设置自动填充类型。 447 448**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 449 450**系统能力:** SystemCapability.ArkUI.ArkUI.Full 451 452**参数:** 453 454| 参数名 | 类型 | 必填 | 说明 | 455| ----------- | ------------------------------------- | ---- | -------------- | 456| contentType | [ContentType](#contenttype12枚举说明) | 是 | 自动填充类型。 | 457 458### lineHeight<sup>12+</sup> 459 460lineHeight(value: number | string | Resource) 461 462设置文本的文本行高,设置值不大于0时,不限制文本行高,自适应字体大小,number类型时单位为fp。 463 464**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 465 466**系统能力:** SystemCapability.ArkUI.ArkUI.Full 467 468**参数:** 469 470| 参数名 | 类型 | 必填 | 说明 | 471| ------ | ------------------------------------------------------------ | ---- | ---------------- | 472| value | number \| string \| [Resource](ts-types.md#resource) | 是 | 文本的文本行高。 | 473 474### decoration<sup>12+</sup> 475 476decoration(value: TextDecorationOptions) 477 478设置文本装饰线类型样式及其颜色。 479 480**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 481 482**系统能力:** SystemCapability.ArkUI.ArkUI.Full 483 484**参数:** 485 486| 参数名 | 类型 | 必填 | 说明 | 487| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 488| value | [TextDecorationOptions](ts-types.md#textdecorationoptions12对象说明) | 是 | 文本装饰线对象。<br />默认值:{<br/> type: TextDecorationType.None,<br/> color: Color.Black,<br/> style: TextDecorationStyle.SOLID <br/>} | 489 490### letterSpacing<sup>12+</sup> 491 492letterSpacing(value: number | string | Resource) 493 494设置文本字符间距。设置该值为百分比时,按默认值显示。设置该值为0时,按默认值显示。 495 496当取值为负值时,文字会发生压缩,负值过小时会将组件内容区大小压缩为0,导致无内容显示。 497 498**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 499 500**系统能力:** SystemCapability.ArkUI.ArkUI.Full 501 502**参数:** 503 504| 参数名 | 类型 | 必填 | 说明 | 505| ------ | -------------------------- | ---- | -------------- | 506| value | number \| string \| [Resource](ts-types.md#resource) | 是 | 文本字符间距。 | 507 508### fontFeature<sup>12+</sup> 509 510fontFeature(value: string) 511 512设置文字特性效果,比如数字等宽的特性。 513 514格式为:normal \| \<feature-tag-value\> 515 516\<feature-tag-value\>的格式为:\<string\> \[ \<integer\> \| on \| off ] 517 518\<feature-tag-value\>的个数可以有多个,中间用','隔开。 519 520例如,使用等宽数字的输入格式为:"ss01" on。 521 522**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 523 524**系统能力:** SystemCapability.ArkUI.ArkUI.Full 525 526**参数:** 527 528| 参数名 | 类型 | 必填 | 说明 | 529| ------ | ------ | ---- | -------------- | 530| value | string | 是 | 文字特性效果。 | 531 532Font Feature当前支持的属性见 [fontFeature属性列表](ts-basic-components-text.md#fontfeature12)。 533设置 Font Feature 属性,Font Feature 是 OpenType 字体的高级排版能力,如支持连字、数字等宽等特性,一般用在自定义字体中,其能力需要字体本身支持。 534更多 Font Feature 能力介绍可参考 https://www.w3.org/TR/css-fonts-3/#font-feature-settings-prop 和 https://sparanoid.com/lab/opentype-features/ 535### wordBreak<sup>12+</sup> 536 537wordBreak(value: WordBreak) 538 539设置文本断行规则。该属性对placeholder文本无效。 540 541**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 542 543**系统能力:** SystemCapability.ArkUI.ArkUI.Full 544 545**参数:** 546 547| 参数名 | 类型 | 必填 | 说明 | 548| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 549| value | [WordBreak](ts-appendix-enums.md#wordbreak11) | 是 | 文本断行规则。 <br />默认值:WordBreak.BREAK_WORD | 550 551> **说明:** 552> 553> 组件不支持clip属性设置,设置该属性任意枚举值对组件文本截断无影响。 554 555### selectedBackgroundColor<sup>12+</sup> 556 557selectedBackgroundColor(value: ResourceColor) 558 559设置文本选中底板颜色。如果未设置不透明度,默认为20%不透明度。 560 561**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 562 563**系统能力:** SystemCapability.ArkUI.ArkUI.Full 564 565**参数:** 566 567| 参数名 | 类型 | 必填 | 说明 | 568| ------ | ------------------------------------------ | ---- | ------------------------------------------ | 569| value | [ResourceColor](ts-types.md#resourcecolor) | 是 | 文本选中底板颜色。<br/>默认为20%不透明度。 | 570 571### caretStyle<sup>12+</sup> 572 573caretStyle(value: CaretStyle) 574 575设置光标风格。 576 577**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 578 579**系统能力:** SystemCapability.ArkUI.ArkUI.Full 580 581**参数:** 582 583| 参数名 | 类型 | 必填 | 说明 | 584| ------ | ----------------------------------- | ---- | ------------ | 585| value | [CaretStyle](ts-text-common.md#caretstyle10) | 是 | 光标的风格。 | 586 587### textIndent<sup>12+</sup> 588 589textIndent(value: Dimension) 590 591设置首行文本缩进。 592 593**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 594 595**系统能力:** SystemCapability.ArkUI.ArkUI.Full 596 597**参数:** 598 599| 参数名 | 类型 | 必填 | 说明 | 600| ------ | ----------------------------------- | ---- | ---------------------------- | 601| value | [Dimension](ts-types.md#dimension10)| 是 | 首行文本缩进。<br/>默认值:0 | 602 603### textOverflow<sup>12+</sup> 604 605textOverflow(value: TextOverflow) 606 607设置文本超长时的显示方式。 608 609内联模式,主动配置textoverflow才会生效按maxline截断效果,不配置时,默认不截断。 610 611文本截断是按字截断。例如,英文以单词为最小单位进行截断,若需要以字母为单位进行截断,wordBreak属性可设置为WordBreak.BREAK_ALL。 612 613当overflow设置为TextOverflow.None、TextOverflow.Clip、TextOverflow.Ellipsis时,需配合maxLines使用,单独设置不生效。设置TextOverflow.None与TextOverflow.Clip效果一样。 614 615**卡片能力:** 该接口支持在ArkTS卡片中使用。 616 617**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 618 619**系统能力:** SystemCapability.ArkUI.ArkUI.Full 620 621**参数:** 622 623| 参数名 | 类型 | 必填 | 说明 | 624| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 625| value | [TextOverflow](ts-appendix-enums.md#textoverflow) | 是 | 文本超长时的显示方式。<br/>默认值:TextOverflow.Clip | 626 627> **说明:** 628> TextArea组件不支持设置TextOverflow.MARQUEE模式,当设置为TextOverflow.MARQUEE模式时 显示为TextOverflow.Clip 629 630### minFontSize<sup>12+</sup> 631 632minFontSize(value: number | string | Resource) 633 634设置文本最小显示字号。 635 636需配合[maxFontSize](#maxfontsize12)以及[maxLines](#maxlines10)或布局大小限制使用,单独设置不生效。 637 638自适应字号生效时,fontSize设置不生效。 639 640**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 641 642**系统能力:** SystemCapability.ArkUI.ArkUI.Full 643 644**参数:** 645 646| 参数名 | 类型 | 必填 | 说明 | 647| ------ | ------------------------------------------------------------ | ---- | ------------------ | 648| value | number \| string \| [Resource](ts-types.md#resource) | 是 | 文本最小显示字号。 | 649 650### maxFontSize<sup>12+</sup> 651 652maxFontSize(value: number | string | Resource) 653 654设置文本最大显示字号。 655 656需配合[minFontSize](#minfontsize12)以及[maxLines](#maxlines10)或布局大小限制使用,单独设置不生效。 657 658自适应字号生效时,fontSize设置不生效。 659 660**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 661 662**系统能力:** SystemCapability.ArkUI.ArkUI.Full 663 664**参数:** 665 666| 参数名 | 类型 | 必填 | 说明 | 667| ------ | ------------------------------------------------------------ | ---- | ------------------ | 668| value | number \| string \| [Resource](ts-types.md#resource) | 是 | 文本最大显示字号。 | 669 670### heightAdaptivePolicy<sup>12+</sup> 671 672heightAdaptivePolicy(value: TextHeightAdaptivePolicy) 673 674设置文本自适应高度的方式。 675 676当设置为TextHeightAdaptivePolicy.MAX_LINES_FIRST时,优先使用[maxLines](#maxlines10)属性来调整文本高度。如果使用maxLines属性的布局大小超过了布局约束,则尝试在[minFontSize](#minfontsize12)和[maxFontSize](#maxfontsize12)的范围内缩小字体以显示更多文本。 677组件设置为内联输入风格,编辑态与非编辑态存在字体大小不一致情况。 678 679当设置为TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST时,优先使用minFontSize属性来调整文本高度。如果使用minFontSize属性可以将文本布局在一行中,则尝试在minFontSize和maxFontSize的范围内增大字体并使用最大可能的字体大小。 680 681当设置为TextHeightAdaptivePolicy.LAYOUT_CONSTRAINT_FIRST时,优先使用布局约束来调整文本高度。如果布局大小超过布局约束,则尝试在minFontSize和maxFontSize的范围内缩小字体以满足布局约束。 682 683**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 684 685**系统能力:** SystemCapability.ArkUI.ArkUI.Full 686 687**参数:** 688 689| 参数名 | 类型 | 必填 | 说明 | 690| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 691| value | [TextHeightAdaptivePolicy](ts-appendix-enums.md#textheightadaptivepolicy10) | 是 | 文本自适应高度的方式。<br/>默认值:TextHeightAdaptivePolicy.MAX_LINES_FIRST | 692 693### lineSpacing<sup>12+</sup> 694 695lineSpacing(value: LengthMetrics) 696 697设置文本的行间距,设置值不大于0时,取默认值0。 698 699**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 700 701**系统能力:** SystemCapability.ArkUI.ArkUI.Full 702 703**参数:** 704 705| 参数名 | 类型 | 必填 | 说明 | 706| ------ | ------------------------------------------------------------ | ---- | ---------------- | 707| value | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | 是 | 文本的行间距。默认值:0 | 708 709### lineBreakStrategy<sup>12+</sup> 710 711lineBreakStrategy(strategy: LineBreakStrategy) 712 713设置折行规则。该属性在wordBreak不等于breakAll的时候生效,不支持连词符。 714 715**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 716 717**系统能力:** SystemCapability.ArkUI.ArkUI.Full 718 719**参数:** 720 721| 参数名 | 类型 | 必填 | 说明 | 722| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------- | 723| strategy | [LineBreakStrategy](ts-appendix-enums.md#linebreakstrategy12) | 是 | 文本的折行规则。 <br />默认值:LineBreakStrategy.GREEDY | 724 725### editMenuOptions<sup>12+</sup> 726 727editMenuOptions(editMenu: EditMenuOptions) 728 729设置自定义菜单扩展项,允许用户设置扩展项的文本内容、图标、回调方法。 730 731**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 732 733**系统能力:** SystemCapability.ArkUI.ArkUI.Full 734 735**参数:** 736 737| 参数名 | 类型 | 必填 | 说明 | 738| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 739| editMenu | [EditMenuOptions](ts-text-common.md#editmenuoptions) | 是 | 扩展菜单选项。 | 740 741### enablePreviewText<sup>12+</sup> 742 743enablePreviewText(enable: boolean) 744 745设置是否开启输入预上屏。 746 747预上屏内容定义为文字暂存态,目前不支持文字拦截功能,因此不触发onWillInsert、onDidInsert、onWillDelete、onDidDelete回调。 748 749**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 750 751**系统能力:** SystemCapability.ArkUI.ArkUI.Full 752 753**参数:** 754 755| 参数名 | 类型 | 必填 | 说明 | 756| ------ | ------- | ---- | ---------------------------------- | 757| enable | boolean | 是 | 是否开启输入预上屏。<br/>默认值:true | 758 759> **说明:** 760> 761> 该接口在CAPI场景使用时下,默认关闭。可以在工程的module.json5中配置[metadata](../../../../application-dev/quick-start/module-structure.md#metadata对象内部结构)字段控制是否启用预上屏,配置如下: 762> ```json 763> "metadata": [ 764> { 765> "name": "can_preview_text", 766> "value": "true", 767> } 768> ] 769> ``` 770 771### enableHapticFeedback<sup>13+</sup> 772 773enableHapticFeedback(isEnabled: boolean) 774 775设置是否开启触控反馈。 776 777**原子化服务API:** 从API version 13开始,该接口支持在原子化服务中使用。 778 779**系统能力:** SystemCapability.ArkUI.ArkUI.Full 780 781**参数:** 782 783| 参数名 | 类型 | 必填 | 说明 | 784| ------ | ------- | ---- | ---------------------------------- | 785| isEnabled | boolean | 是 | 是否开启触控反馈。<br/>默认值:true | 786 787> **说明:** 788> 789> 开启触控反馈时,需要在工程的module.json5中配置requestPermissions字段开启振动权限,配置如下: 790> ```json 791> "requestPermissions": [ 792> { 793> "name": "ohos.permission.VIBRATE", 794> } 795> ] 796> ``` 797 798## 事件 799 800除支持[通用事件](ts-universal-events-click.md)外,还支持以下事件: 801 802### onChange 803 804onChange(callback: EditableTextOnChangeCallback) 805 806输入内容发生变化时,触发该回调。 807 808在本回调中,若执行了光标操作,需要开发者在预上屏场景下依据previewText参数调整光标逻辑,以适应预上屏场景。 809 810**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 811 812**系统能力:** SystemCapability.ArkUI.ArkUI.Full 813 814**参数:** 815 816| 参数名 | 类型 | 必填 | 说明 | 817| ------ | ------ | ---- | -------------------- | 818| callback | [EditableTextOnChangeCallback](ts-text-common.md#editabletextonchangecallback12) | 是 | 当前输入文本内容变化时的回调。 | 819 820### onEditChange<sup>10+</sup> 821 822onEditChange(callback: (isEditing: boolean) => void) 823 824输入状态变化时,触发该回调。有光标时为编辑态,无光标时为非编辑态。isEditing为true表示正在输入。 825 826**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 827 828**系统能力:** SystemCapability.ArkUI.ArkUI.Full 829 830**参数:** 831 832| 参数名 | 类型 | 必填 | 说明 | 833| --------- | ------- | ---- | -------------------- | 834| isEditing | boolean | 是 | 为true表示正在输入。 | 835 836### onCopy<sup>8+</sup> 837 838onCopy(callback: (value: string) => void) 839 840进行复制操作时,触发该回调。 841 842**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 843 844**系统能力:** SystemCapability.ArkUI.ArkUI.Full 845 846**参数:** 847 848| 参数名 | 类型 | 必填 | 说明 | 849| ------ | ------ | ---- | ---------------- | 850| value | string | 是 | 复制的文本内容。 | 851 852### onCut<sup>8+</sup> 853 854onCut(callback: (value: string) => void) 855 856进行剪切操作时,触发该回调。 857 858**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 859 860**系统能力:** SystemCapability.ArkUI.ArkUI.Full 861 862**参数:** 863 864| 参数名 | 类型 | 必填 | 说明 | 865| ------ | ------ | ---- | ---------------- | 866| value | string | 是 | 剪切的文本内容。 | 867 868### onPaste 869 870onPaste(callback: (value: string, event: PasteEvent) => void) 871 872进行粘贴操作时,触发该回调。 873 874**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 875 876**系统能力:** SystemCapability.ArkUI.ArkUI.Full 877 878**参数:** 879 880| 参数名 | 类型 | 必填 | 说明 | 881| ------------------- | ------------------------------------------------------------ | ---- | ---------------------- | 882| value | string | 是 | 粘贴的文本内容。 | 883| event<sup>11+</sup> | [PasteEvent](ts-basic-components-richeditor.md#pasteevent11) | 是 | 用户自定义的粘贴事件。 | 884 885### onTextSelectionChange<sup>10+</sup> 886 887onTextSelectionChange(callback: (selectionStart: number, selectionEnd: number) => void) 888 889文本选择的位置发生变化或编辑状态下光标位置发生变化时,触发该回调。 890 891**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 892 893**系统能力:** SystemCapability.ArkUI.ArkUI.Full 894 895**参数:** 896 897| 参数名 | 类型 | 必填 | 说明 | 898| -------------- | ------ | ---- | --------------------------------------- | 899| selectionStart | number | 是 | 所选文本的起始位置,文字的起始位置为0。 | 900| selectionEnd | number | 是 | 所选文本的结束位置。 | 901 902### onContentScroll<sup>10+</sup> 903 904onContentScroll(callback: (totalOffsetX: number, totalOffsetY: number) => void) 905 906文本内容滚动时,触发该回调。 907 908**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 909 910**系统能力:** SystemCapability.ArkUI.ArkUI.Full 911 912**参数:** 913 914| 参数名 | 类型 | 必填 | 说明 | 915| ------------ | ------ | ---- | ---------------------------------- | 916| totalOffsetX | number | 是 | 文本在内容区的横坐标偏移,单位px。 | 917| totalOffsetY | number | 是 | 文本在内容区的纵坐标偏移,单位px。 | 918 919### onSubmit<sup>11+</sup> 920 921onSubmit(callback: (enterKey: EnterKeyType) => void) 922 923按下软键盘输入法回车键触发该回调。 924 925**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 926 927**系统能力:** SystemCapability.ArkUI.ArkUI.Full 928 929**参数:** 930 931| 参数名 | 类型 | 必填 | 说明 | 932| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | 933| enterKey | [EnterKeyType](ts-basic-components-textinput.md#enterkeytype枚举说明) | 是 | 输入法回车键类型,类型为EnterKeyType.NEW_LINE时不触发onSubmit。 | 934 935### onSubmit<sup>14+</sup> 936 937onSubmit(callback: TextAreaSubmitCallback) 938 939按下软键盘输入法回车键触发该回调事件,提交事件时提供保持TextArea编辑状态的方法。 940 941**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 942 943**系统能力:** SystemCapability.ArkUI.ArkUI.Full 944 945**参数:** 946 947| 参数名 | 类型 | 必填 | 说明 | 948| ------ | ------- | ---- | ----------------------------- | 949| callback | [TextAreaSubmitCallback](#textareasubmitcallback14) | 是 | 按下软键盘输入法回车键时的回调事件。 | 950 951### onWillInsert<sup>12+</sup> 952 953onWillInsert(callback: Callback\<InsertValue, boolean>) 954 955在将要输入时,触发该回调。 956 957**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 958 959**系统能力:** SystemCapability.ArkUI.ArkUI.Full 960 961**参数:** 962 963| 参数名 | 类型 | 必填 | 说明 | 964| ------ | ------------------------------------------------------------ | ---- | ------------------ | 965| callback | Callback\<[InsertValue](ts-text-common.md#insertvalue12对象说明), boolean> | 是 | 在将要输入时调用的回调。<br/>在返回true时,表示正常插入,返回false时,表示不插入。<br/>在预上屏操作时,该回调不触发。<br/>仅支持系统输入法输入的场景。 | 966 967### onDidInsert<sup>12+</sup> 968 969onDidInsert(callback: Callback\<InsertValue>) 970 971在输入完成时,触发该回调。 972 973**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 974 975**系统能力:** SystemCapability.ArkUI.ArkUI.Full 976 977**参数:** 978 979| 参数名 | 类型 | 必填 | 说明 | 980| ------ | ------------------------------------------------------------ | ---- | ------------------ | 981| callback | Callback\<[InsertValue](ts-text-common.md#insertvalue12对象说明)> | 是 | 在输入完成时调用的回调。<br/>仅支持系统输入法输入的场景。 | 982 983### onWillDelete<sup>12+</sup> 984 985onWillDelete(callback: Callback\<DeleteValue, boolean>) 986 987在将要删除时,触发该回调。 988 989**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 990 991**系统能力:** SystemCapability.ArkUI.ArkUI.Full 992 993**参数:** 994 995| 参数名 | 类型 | 必填 | 说明 | 996| ------ | ------------------------------------------------------------ | ---- | ------------------ | 997| callback | Callback\<[DeleteValue](ts-text-common.md#deletevalue12对象说明), boolean> | 是 | 在将要删除时调用的回调。<br/>在返回true时,表示正常删除,返回false时,表示不删除。<br/>在预上屏删除操作时,该回调不触发。<br/>仅支持系统输入法输入的场景。 | 998 999### onDidDelete<sup>12+</sup> 1000 1001onDidDelete(callback: Callback\<DeleteValue>) 1002 1003在删除完成时,触发该回调。 1004 1005**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1006 1007**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1008 1009**参数:** 1010 1011| 参数名 | 类型 | 必填 | 说明 | 1012| ------ | ------------------------------------------------------------ | ---- | ------------------ | 1013| callback | Callback\<[DeleteValue](ts-text-common.md#deletevalue12对象说明)> | 是 | 在删除完成时调用的回调。<br/>仅支持系统输入法输入的场景。 | 1014 1015## TextAreaController<sup>8+</sup> 1016 1017TextArea组件的控制器继承自[TextContentControllerBase](ts-types.md#textcontentcontrollerbase10)。 1018 1019**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1020 1021**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1022 1023### 导入对象 1024 1025``` 1026controller: TextAreaController = new TextAreaController() 1027``` 1028 1029### constructor<sup>8+</sup> 1030 1031constructor() 1032 1033TextAreaController的构造函数。 1034 1035**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1036 1037**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1038 1039### caretPosition<sup>8+</sup> 1040 1041caretPosition(value: number): void 1042 1043设置输入光标的位置。 1044 1045**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1046 1047**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1048 1049**参数:** 1050 1051| 参数名 | 类型 | 必填 | 说明 | 1052| ------ | ---- | ---- | ---- | 1053| value | number | 是 | 从字符串开始到光标所在位置的字符长度。 | 1054 1055### setTextSelection<sup>10+</sup> 1056 1057setTextSelection(selectionStart: number, selectionEnd: number, options?: SelectionOptions): void 1058 1059组件在获焦状态下,调用该接口设置文本选择区域并高亮显示,且只有在selectionStart小于selectionEnd时,文字才会被选取、高亮显示。 1060 1061**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1062 1063**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1064 1065**参数:** 1066 1067| 参数名 | 类型 | 必填 | 说明 | 1068| ------ | ---- | ---- | ---- | 1069| selectionStart | number | 是 | 文本选择区域起始位置,文本框中文字的起始位置为0。<br/>当selectionStart小于0时、按照0处理;当selectionStart大于文字最大长度时、按照文字最大长度处理。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1070| selectionEnd | number | 是 | 文本选择区域结束位置。<br/>当selectionEnd小于0时、按照0处理;当selectionEnd大于文字最大长度时、按照文字最大长度处理。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1071| options<sup>12+</sup> | [SelectionOptions](ts-types.md#selectionoptions12对象说明) | 否 | 选中文字时的配置。<br />默认值:MenuPolicy.DEFAULT<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1072 1073> **说明:** 1074> 1075> 如果selectionMenuHidden被赋值为true或设备为2in1时,即使options被赋值为MenuPolicy.SHOW,调用setTextSelection也不弹出菜单。 1076> 1077> 如果选中的文本含有emoji表情时,表情的起始位置包含在设置的文本选中区域内就会被选中。 1078 1079### stopEditing<sup>10+</sup> 1080 1081stopEditing(): void 1082 1083退出编辑态。 1084 1085**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1086 1087**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1088 1089## TextAreaType<sup>11+</sup>枚举说明 1090 1091**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1092 1093**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1094 1095| 名称 | 值 | 说明 | 1096| ------ | ----- | ------ | 1097| NORMAL | 0 | 基本输入模式。<br/>支持输入数字、字母、下划线、空格、特殊字符。 | 1098| NUMBER | 2 | 纯数字输入模式。 | 1099| PHONE_NUMBER | 3 | 电话号码输入模式。<br/>支持输入数字、空格、+ 、-、*、#、(、),长度不限。 | 1100| EMAIL | 5 | 邮箱地址输入模式。<br/>支持数字,字母,下划线、小数点、!、#、$、%、&、'、*、+、-、/、=、?、^、`、\{、\|、\}、~,以及@字符(只能存在一个@字符)。 | 1101| NUMBER_DECIMAL<sup>12+</sup> | 12 | 带小数点的数字输入模式。<br/>支持数字,小数点(只能存在一个小数点)。| 1102| URL<sup>12+</sup> | 13 | 带URL的输入模式。 | 1103 1104## ContentType<sup>12+</sup>枚举说明 1105 1106自动填充类型。 1107 1108**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1109 1110**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1111 1112| 名称 | 值 | 说明 | 1113| -------------------------- | ---- | ------------------------------------------------------------ | 1114| USER_NAME | 0 | 【用户名】在已启用密码保险箱的情况下,支持用户名的自动保存和自动填充。 | 1115| PASSWORD | 1 | 【密码】在已启用密码保险箱的情况下,支持密码的自动保存和自动填充。 | 1116| NEW_PASSWORD | 2 | 【新密码】在已启用密码保险箱的情况下,支持自动生成新密码。 | 1117| FULL_STREET_ADDRESS | 3 | 【详细地址】在已启用情景化自动填充的情况下,支持详细地址的自动保存和自动填充。 | 1118| HOUSE_NUMBER | 4 | 【门牌号】在已启用情景化自动填充的情况下,支持门牌号的自动保存和自动填充。 | 1119| DISTRICT_ADDRESS | 5 | 【区/县】在已启用情景化自动填充的情况下,支持区/县的自动保存和自动填充。 | 1120| CITY_ADDRESS | 6 | 【市】在已启用情景化自动填充的情况下,支持市的自动保存和自动填充。 | 1121| PROVINCE_ADDRESS | 7 | 【省】在已启用情景化自动填充的情况下,支持省的自动保存和自动填充。 | 1122| COUNTRY_ADDRESS | 8 | 【国家】在已启用情景化自动填充的情况下,支持国家的自动保存和自动填充。 | 1123| PERSON_FULL_NAME | 9 | 【姓名】在已启用情景化自动填充的情况下,支持姓名的自动保存和自动填充。 | 1124| PERSON_LAST_NAME | 10 | 【姓氏】在已启用情景化自动填充的情况下,支持姓氏的自动保存和自动填充。 | 1125| PERSON_FIRST_NAME | 11 | 【名字】在已启用情景化自动填充的情况下,支持名字的自动保存和自动填充。 | 1126| PHONE_NUMBER | 12 | 【手机号码】在已启用情景化自动填充的情况下,支持手机号码的自动保存和自动填充。 | 1127| PHONE_COUNTRY_CODE | 13 | 【国家代码】在已启用情景化自动填充的情况下,支持国家代码的自动保存和自动填充。 | 1128| FULL_PHONE_NUMBER | 14 | 【包含国家代码的手机号码】在已启用情景化自动填充的情况下,支持包含国家代码的手机号码的自动保存和自动填充。 | 1129| EMAIL_ADDRESS | 15 | 【邮箱地址】在已启用情景化自动填充的情况下,支持邮箱地址的自动保存和自动填充。 | 1130| BANK_CARD_NUMBER | 16 | 【银行卡号】在已启用情景化自动填充的情况下,支持银行卡号的自动保存和自动填充。 | 1131| ID_CARD_NUMBER | 17 | 【身份证号】在已启用情景化自动填充的情况下,支持身份证号的自动保存和自动填充。 | 1132| NICKNAME | 23 | 【昵称】在已启用情景化自动填充的情况下,支持昵称的自动保存和自动填充。 | 1133| DETAIL_INFO_WITHOUT_STREET | 24 | 【无街道地址】在已启用情景化自动填充的情况下,支持无街道地址的自动保存和自动填充。 | 1134| FORMAT_ADDRESS | 25 | 【标准地址】在已启用情景化自动填充的情况下,支持标准地址的自动保存和自动填充。 | 1135 1136## TextAreaSubmitCallback<sup>14+</sup> 1137 1138type TextAreaSubmitCallback = (enterKeyType: EnterKeyType, event?: SubmitEvent) => void 1139 1140软键盘按下回车键时的回调事件。 1141 1142**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 1143 1144**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1145 1146**参数:** 1147 1148| 参数名 | 类型 | 必填 | 说明 | 1149| -------- | ------------------------------------------------------------ | ---- | -------------------------------------------------------- | 1150| enterKeyType | [EnterKeyType](ts-basic-components-textinput.md#enterkeytype枚举说明) | 是 | 软键盘输入法回车键类型。 | 1151| event | [SubmitEvent](ts-basic-components-textinput.md#submitevent11) | 否 | 提交事件。 | 1152 1153## 示例 1154 1155### 示例1(设置与获取光标位置) 1156 1157该示例通过controller实现了光标位置的设置与获取。 1158 1159```ts 1160// xxx.ets 1161@Entry 1162@Component 1163struct TextAreaExample { 1164 @State text: string = '' 1165 @State positionInfo: CaretOffset = { index: 0, x: 0, y: 0 } 1166 controller: TextAreaController = new TextAreaController() 1167 1168 build() { 1169 Column() { 1170 TextArea({ 1171 text: this.text, 1172 placeholder: 'The text area can hold an unlimited amount of text. input your word...', 1173 controller: this.controller 1174 }) 1175 .placeholderFont({ size: 16, weight: 400 }) 1176 .width(336) 1177 .height(56) 1178 .margin(20) 1179 .fontSize(16) 1180 .fontColor('#182431') 1181 .backgroundColor('#FFFFFF') 1182 .onChange((value: string) => { 1183 this.text = value 1184 }) 1185 Text(this.text) 1186 Button('Set caretPosition 1') 1187 .backgroundColor('#007DFF') 1188 .margin(15) 1189 .onClick(() => { 1190 // 设置光标位置到第一个字符后 1191 this.controller.caretPosition(1) 1192 }) 1193 Button('Get CaretOffset') 1194 .backgroundColor('#007DFF') 1195 .margin(15) 1196 .onClick(() => { 1197 this.positionInfo = this.controller.getCaretOffset() 1198 }) 1199 }.width('100%').height('100%').backgroundColor('#F1F3F5') 1200 } 1201} 1202``` 1203 1204 1205 1206### 示例2(设置计数器) 1207 1208该示例通过maxLength、showCounter属性实现了计数器的功能。 1209 1210```ts 1211// xxx.ets 1212@Entry 1213@Component 1214struct TextAreaExample { 1215 @State text: string = '' 1216 controller: TextAreaController = new TextAreaController() 1217 1218 build() { 1219 Column() { 1220 TextArea({ 1221 text: this.text, 1222 placeholder: 'The text area can hold an unlimited amount of text. input your word...', 1223 controller: this.controller 1224 }) 1225 .placeholderFont({ size: 16, weight: 400 }) 1226 .width(336) 1227 .height(56) 1228 .margin(20) 1229 .fontSize(16) 1230 .fontColor('#182431') 1231 .backgroundColor('#FFFFFF') 1232 .maxLength(4) 1233 .showCounter(true, { thresholdPercentage: 50, highlightBorder: true }) 1234 //计数器显示效果为用户当前输入字符数/最大字符限制数。最大字符限制数通过maxLength()接口设置。 1235 //如果用户当前输入字符数达到最大字符限制乘50%(thresholdPercentage)。字符计数器显示。 1236 //用户设置highlightBorder为false时,配置取消红色边框。不设置此参数时,默认为true。 1237 .onChange((value: string) => { 1238 this.text = value 1239 }) 1240 }.width('100%').height('100%').backgroundColor('#F1F3F5') 1241 } 1242} 1243``` 1244 1245 1246 1247 1248### 示例3(设置自定义键盘) 1249 1250该示例通过customKeyboard属性实现了自定义键盘的功能。 1251 1252```ts 1253// xxx.ets 1254@Entry 1255@Component 1256struct TextAreaExample { 1257 controller: TextAreaController = new TextAreaController() 1258 @State inputValue: string = "" 1259 1260 // 自定义键盘组件 1261 @Builder CustomKeyboardBuilder() { 1262 Column() { 1263 Button('x').onClick(() => { 1264 // 关闭自定义键盘 1265 this.controller.stopEditing() 1266 }) 1267 Grid() { 1268 ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'], (item: number | string) => { 1269 GridItem() { 1270 Button(item + "") 1271 .width(110).onClick(() => { 1272 this.inputValue += item 1273 }) 1274 } 1275 }) 1276 }.maxCount(3).columnsGap(10).rowsGap(10).padding(5) 1277 }.backgroundColor(Color.Gray) 1278 } 1279 1280 build() { 1281 Column() { 1282 TextArea({ controller: this.controller, text: this.inputValue}) 1283 // 绑定自定义键盘 1284 .customKeyboard(this.CustomKeyboardBuilder()).margin(10).border({ width: 1 }) 1285 .height(200) 1286 } 1287 } 1288} 1289``` 1290 1291 1292 1293### 示例4(设置输入法回车键类型) 1294 1295该示例通过enterKeyType属性实现了动态切换输入法回车键的效果。 1296 1297```ts 1298// xxx.ets 1299@Entry 1300@Component 1301struct TextAreaExample { 1302 @State text: string = '' 1303 @State enterTypes: Array<EnterKeyType> = 1304 [EnterKeyType.Go, EnterKeyType.Search, EnterKeyType.Send, EnterKeyType.Done, EnterKeyType.Next, 1305 EnterKeyType.PREVIOUS, EnterKeyType.NEW_LINE] 1306 @State index: number = 0 1307 1308 build() { 1309 Column({ space: 20 }) { 1310 TextArea({ placeholder: '请输入用户名', text: this.text }) 1311 .width(380) 1312 .enterKeyType(this.enterTypes[this.index]) 1313 .onChange((value: string) => { 1314 this.text = value 1315 }) 1316 .onSubmit((enterKey: EnterKeyType) => { 1317 console.log("trigger area onsubmit" + enterKey); 1318 }) 1319 Button('改变EnterKeyType').onClick(() => { 1320 this.index = (this.index + 1) % this.enterTypes.length; 1321 }) 1322 1323 }.width('100%') 1324 } 1325} 1326``` 1327 1328 1329 1330 1331### 示例5(设置文本断行规则) 1332 1333该示例通过wordBreak属性实现了TextArea不同断行规则下的效果。 1334 1335```ts 1336// xxx.ets 1337@Entry 1338@Component 1339struct TextAreaExample { 1340 build() { 1341 Column() { 1342 Text("属性WordBreakType为NORMAL的样式:").fontSize(16).fontColor(0xFF0000) 1343 TextArea({ 1344 text: 'This is set wordBreak to WordBreak text Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu.' 1345 }) 1346 .fontSize(16) 1347 .border({ width: 1 }) 1348 .wordBreak(WordBreak.NORMAL) 1349 Text("英文文本,属性WordBreakType为BREAK_ALL的样式:").fontSize(16).fontColor(0xFF0000) 1350 TextArea({ 1351 text: 'This is set wordBreak to WordBreak text Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu.' 1352 }) 1353 .fontSize(16) 1354 .border({ width: 1 }) 1355 .wordBreak(WordBreak.BREAK_ALL) 1356 Text("中文文本,属性WordBreakType为BREAK_ALL的样式:").fontSize(16).fontColor(0xFF0000) 1357 TextArea({ 1358 text: '多行文本输入框组件,当输入的文本内容超过组件宽度时会自动换行显示。\n高度未设置时,组件无默认高度,自适应内容高度。宽度未设置时,默认撑满最大宽度。' 1359 }) 1360 .fontSize(16) 1361 .border({ width: 1 }) 1362 .wordBreak(WordBreak.BREAK_ALL) 1363 Text("属性WordBreakType为BREAK_WORD的样式:").fontSize(16).fontColor(0xFF0000) 1364 TextArea({ 1365 text: 'This is set wordBreak to WordBreak text Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu.' 1366 }) 1367 .fontSize(16) 1368 .border({ width: 1 }) 1369 .wordBreak(WordBreak.BREAK_WORD) 1370 } 1371 } 1372} 1373``` 1374 1375 1376 1377### 示例6(设置文本样式) 1378 1379该示例通过lineHeight、letterSpacing、decoration属性展示了不同样式的文本效果。 1380 1381```ts 1382// xxx.ets 1383@Entry 1384@Component 1385struct TextAreaExample { 1386 build() { 1387 Row() { 1388 Column() { 1389 Text('lineHeight').fontSize(9).fontColor(0xCCCCCC) 1390 TextArea({text: 'lineHeight unset'}) 1391 .border({ width: 1 }).padding(10).margin(5) 1392 TextArea({text: 'lineHeight 15'}) 1393 .border({ width: 1 }).padding(10).margin(5).lineHeight(15) 1394 TextArea({text: 'lineHeight 30'}) 1395 .border({ width: 1 }).padding(10).margin(5).lineHeight(30) 1396 1397 Text('letterSpacing').fontSize(9).fontColor(0xCCCCCC) 1398 TextArea({text: 'letterSpacing 0'}) 1399 .border({ width: 1 }).padding(5).margin(5).letterSpacing(0) 1400 TextArea({text: 'letterSpacing 3'}) 1401 .border({ width: 1 }).padding(5).margin(5).letterSpacing(3) 1402 TextArea({text: 'letterSpacing -1'}) 1403 .border({ width: 1 }).padding(5).margin(5).letterSpacing(-1) 1404 1405 Text('decoration').fontSize(9).fontColor(0xCCCCCC) 1406 TextArea({text: 'LineThrough, Red\nsecond line'}) 1407 .border({ width: 1 }).padding(5).margin(5) 1408 .decoration({type: TextDecorationType.LineThrough, color: Color.Red}) 1409 TextArea({text: 'Overline, Red, DOTTED\nsecond line'}) 1410 .border({ width: 1 }).padding(5).margin(5) 1411 .decoration({type: TextDecorationType.Overline, color: Color.Red, style: TextDecorationStyle.DOTTED}) 1412 TextArea({text: 'Underline, Red, WAVY\nsecond line'}) 1413 .border({ width: 1 }).padding(5).margin(5) 1414 .decoration({type: TextDecorationType.Underline, color: Color.Red, style: TextDecorationStyle.WAVY}) 1415 }.height('90%') 1416 } 1417 .width('90%') 1418 .margin(10) 1419 } 1420} 1421``` 1422 1423 1424 1425### 示例7(设置文字特性效果) 1426 1427该示例通过fontFeature属性实现了文本在不同文字特性下的展示效果。 1428 1429```ts 1430// xxx.ets 1431@Entry 1432@Component 1433struct TextAreaExample { 1434 @State text1: string = 'This is ss01 on : 0123456789' 1435 @State text2: string = 'This is ss01 off: 0123456789' 1436 1437 build() { 1438 Column(){ 1439 TextArea({text: this.text1}) 1440 .fontSize(20) 1441 .margin({top:200}) 1442 .fontFeature("\"ss01\" on") 1443 TextArea({text : this.text2}) 1444 .margin({top:10}) 1445 .fontSize(20) 1446 .fontFeature("\"ss01\" off") 1447 } 1448 .width("90%") 1449 .margin("5%") 1450 } 1451} 1452``` 1453 1454 1455### 示例8(自定义键盘避让) 1456 1457该示例通过自定义键盘实现了键盘避让的功能。 1458 1459```ts 1460// xxx.ets 1461@Entry 1462@Component 1463struct TextAreaExample { 1464 controller: TextAreaController = new TextAreaController() 1465 @State inputValue: string = "" 1466 @State height1:string|number = '80%' 1467 @State height2:number = 100 1468 @State supportAvoidance:boolean = true; 1469 1470 // 自定义键盘组件 1471 @Builder CustomKeyboardBuilder() { 1472 Column() { 1473 Row(){ 1474 Button('x').onClick(() => { 1475 // 关闭自定义键盘 1476 this.controller.stopEditing() 1477 }).margin(10) 1478 } 1479 Grid() { 1480 ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'], (item: number | string) => { 1481 GridItem() { 1482 Button(item + "") 1483 .width(110).onClick(() => { 1484 this.inputValue += item 1485 }) 1486 } 1487 }) 1488 }.maxCount(3).columnsGap(10).rowsGap(10).padding(5) 1489 }.backgroundColor(Color.Gray) 1490 } 1491 1492 build() { 1493 Column() { 1494 Row(){ 1495 Button("20%") 1496 .fontSize(24) 1497 .onClick(()=>{ 1498 this.height1 = "20%" 1499 }) 1500 Button("80%") 1501 .fontSize(24) 1502 .margin({left:20}) 1503 .onClick(()=>{ 1504 this.height1 = "80%" 1505 }) 1506 } 1507 .justifyContent(FlexAlign.Center) 1508 .alignItems(VerticalAlign.Bottom) 1509 .height(this.height1) 1510 .width("100%") 1511 .padding({bottom:50}) 1512 TextArea({ controller: this.controller, text: this.inputValue}) 1513 .height(100) 1514 // 绑定自定义键盘 1515 .customKeyboard(this.CustomKeyboardBuilder(),{ supportAvoidance: this.supportAvoidance }).margin(10).border({ width: 1 }) 1516 // .height(200) 1517 } 1518 } 1519} 1520``` 1521 1522 1523### 示例9(设置文本自适应) 1524 1525该示例通过minFontSize、maxFontSize、heightAdaptivePolicy属性展示了文本自适应字号的效果。 1526 1527```ts 1528// xxx.ets 1529@Entry 1530@Component 1531struct TextAreaExample { 1532 build() { 1533 Row() { 1534 Column() { 1535 Text('heightAdaptivePolicy').fontSize(9).fontColor(0xCCCCCC) 1536 TextArea({text: 'This is the text with the height adaptive policy set'}) 1537 .width('80%').height(90).borderWidth(1).margin(1) 1538 .minFontSize(4) 1539 .maxFontSize(40) 1540 .maxLines(3) 1541 .heightAdaptivePolicy(TextHeightAdaptivePolicy.MAX_LINES_FIRST) 1542 TextArea({text: 'This is the text with the height adaptive policy set'}) 1543 .width('80%').height(90).borderWidth(1).margin(1) 1544 .minFontSize(4) 1545 .maxFontSize(40) 1546 .maxLines(3) 1547 .heightAdaptivePolicy(TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST) 1548 TextArea({text: 'This is the text with the height adaptive policy set'}) 1549 .width('80%').height(90).borderWidth(1).margin(1) 1550 .minFontSize(4) 1551 .maxFontSize(40) 1552 .maxLines(3) 1553 .heightAdaptivePolicy(TextHeightAdaptivePolicy.LAYOUT_CONSTRAINT_FIRST) 1554 }.height('90%') 1555 } 1556 .width('90%') 1557 .margin(10) 1558 } 1559} 1560``` 1561 1562 1563 1564### 示例10(设置文本行间距) 1565 1566该示例通过lineSpacing属性展示了文本在不同行间距下的展示效果。 1567 1568```ts 1569// xxx.ets 1570import { LengthMetrics } from '@kit.ArkUI' 1571 1572@Entry 1573@Component 1574struct TextAreaExample { 1575 build() { 1576 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) { 1577 Text('TextArea lineSpacing.').fontSize(9).fontColor(0xCCCCCC) 1578 TextArea({ placeholder: 'This is the TextArea with no lineSpacing set.' }) 1579 .fontSize(12) 1580 TextArea({ placeholder: 'This is the TextArea with lineSpacing set to 20_px.' }) 1581 .fontSize(12) 1582 .lineSpacing(LengthMetrics.px(20)) 1583 TextArea({ placeholder: 'This is the TextArea with lineSpacing set to 20_vp.' }) 1584 .fontSize(12) 1585 .lineSpacing(LengthMetrics.vp(20)) 1586 TextArea({ placeholder: 'This is the TextArea with lineSpacing set to 20_fp.' }) 1587 .fontSize(12) 1588 .lineSpacing(LengthMetrics.fp(20)) 1589 TextArea({ placeholder: 'This is the TextArea with lineSpacing set to 20_lpx.' }) 1590 .fontSize(12) 1591 .lineSpacing(LengthMetrics.lpx(20)) 1592 TextArea({ placeholder: 'This is the TextArea with lineSpacing set to 100%.' }) 1593 .fontSize(12) 1594 .lineSpacing(LengthMetrics.percent(1)) 1595 }.height(600).width(350).padding({ left: 35, right: 35, top: 35 }) 1596 } 1597} 1598``` 1599 1600 1601 1602### 示例11(设置自动填充) 1603 1604该示例通过contentType、enableAutoFill属性实现了文本自动填充的功能。 1605 1606```ts 1607// xxx.ets 1608@Entry 1609@Component 1610struct TextAreaExample { 1611 @State text: string = '' 1612 1613 build() { 1614 Column() { 1615 // 邮箱地址自动填充类型 1616 TextArea({ placeholder: 'input your email...' }) 1617 .width('95%') 1618 .height(40) 1619 .margin(20) 1620 .contentType(ContentType.EMAIL_ADDRESS) 1621 .enableAutoFill(true) 1622 .maxLength(20) 1623 // 街道地址自动填充类型 1624 TextArea({ placeholder: 'input your street address...' }) 1625 .width('95%') 1626 .height(40) 1627 .margin(20) 1628 .contentType(ContentType.FULL_STREET_ADDRESS) 1629 .enableAutoFill(true) 1630 .maxLength(20) 1631 }.width('100%').height('100%').backgroundColor('#F1F3F5') 1632 } 1633} 1634``` 1635 1636 1637 1638### 示例12(设置折行规则) 1639 1640该示例通过lineBreakStrategy属性实现了TextArea不同折行规则下的效果。 1641 1642```ts 1643// xxx.ets 1644@Entry 1645@Component 1646struct TextAreaExample { 1647 @State message1: string = 1648 "They can be classified as built-in components–those directly provided by the ArkUI framework and custom components – those defined by developers" + 1649 "The built-in components include buttons radio buttonsprogress indicators and text You can set the rendering effectof thesecomponents in method chaining mode," + 1650 "page components are divided into independent UI units to implementindependent creation development and reuse of different units on pages making pages more engineering-oriented." 1651 @State lineBreakStrategyIndex: number = 0 1652 @State lineBreakStrategy: LineBreakStrategy[] = 1653 [LineBreakStrategy.GREEDY, LineBreakStrategy.HIGH_QUALITY, LineBreakStrategy.BALANCED] 1654 @State lineBreakStrategyStr: string[] = ['GREEDY', 'HIGH_QUALITY', 'BALANCED'] 1655 1656 build() { 1657 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start }) { 1658 Text('lineBreakStrategy').fontSize(9).fontColor(0xCCCCCC) 1659 TextArea({ text: this.message1 }) 1660 .fontSize(12) 1661 .border({ width: 1 }) 1662 .padding(10) 1663 .width('100%') 1664 .lineBreakStrategy(this.lineBreakStrategy[this.lineBreakStrategyIndex]) 1665 Row() { 1666 Button('当前lineBreakStrategy模式:' + this.lineBreakStrategyStr[this.lineBreakStrategyIndex]).onClick(() => { 1667 this.lineBreakStrategyIndex++ 1668 if (this.lineBreakStrategyIndex > (this.lineBreakStrategyStr.length - 1)) { 1669 this.lineBreakStrategyIndex = 0 1670 } 1671 }) 1672 }.padding({ top: 10 }) 1673 }.height(700).width(370).padding({ left: 35, right: 35, top: 35 }) 1674 } 1675} 1676``` 1677 1678 1679 1680### 示例13(支持插入和删除回调) 1681 1682该示例通过onWillInsert、onDidInsert、onWillDelete、onDidDelete接口实现了插入和删除的功能。 1683 1684```ts 1685// xxx.ets 1686@Entry 1687@Component 1688struct TextAreaExample { 1689 @State insertValue: string = "" 1690 @State deleteValue: string = "" 1691 @State insertOffset: number = 0 1692 @State deleteOffset: number = 0 1693 @State deleteDirection: number = 0 1694 1695 build() { 1696 Row() { 1697 Column() { 1698 TextArea({ text: "TextArea支持插入回调文本" }) 1699 .width(300) 1700 .height(60) 1701 .onWillInsert((info: InsertValue) => { 1702 this.insertValue = info.insertValue 1703 return true; 1704 }) 1705 .onDidInsert((info: InsertValue) => { 1706 this.insertOffset = info.insertOffset 1707 }) 1708 1709 Text("insertValue:" + this.insertValue + " insertOffset:" + this.insertOffset).height(30) 1710 1711 TextArea({ text: "TextArea支持删除回调文本b" }) 1712 .width(300) 1713 .height(60) 1714 .onWillDelete((info: DeleteValue) => { 1715 this.deleteValue = info.deleteValue 1716 info.direction 1717 return true; 1718 }) 1719 .onDidDelete((info: DeleteValue) => { 1720 this.deleteOffset = info.deleteOffset 1721 this.deleteDirection = info.direction 1722 }) 1723 1724 Text("deleteValue:" + this.deleteValue + " deleteOffset:" + this.deleteOffset).height(30) 1725 Text("deleteDirection:" + (this.deleteDirection == 0 ? "BACKWARD" : "FORWARD")).height(30) 1726 1727 }.width('100%') 1728 } 1729 .height('100%') 1730 } 1731} 1732``` 1733 1734 1735 1736### 示例14(文本扩展自定义菜单) 1737 1738该示例通过editMenuOptions接口实现了文本设置自定义菜单扩展项的文本内容、图标以及回调的功能。 1739 1740```ts 1741// xxx.ets 1742@Entry 1743@Component 1744struct TextAreaExample { 1745 @State text: string = 'TextArea 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 TextArea({ text: this.text }) 1783 .width('95%') 1784 .height(56) 1785 .editMenuOptions(this.editMenuOptions) 1786 .margin({ top: 100 }) 1787 } 1788 .width("90%") 1789 .margin("5%") 1790 } 1791} 1792``` 1793 1794 1795 1796