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-component-general-attributes.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时,按默认值显示。string类型支持number类型取值的字符串形式,可以附带单位,例如"10"、"10fp"。 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) | 是 | 文本字符间距。<br/>单位:fp | 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设置文本最小显示字号。string类型支持number类型取值的字符串形式,可以附带单位,例如"10"、"10fp"。 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) | 是 | 文本最小显示字号。<br/>单位:fp | 649 650### maxFontSize<sup>12+</sup> 651 652maxFontSize(value: number | string | Resource) 653 654设置文本最大显示字号。string类型支持number类型取值的字符串形式,可以附带单位,例如"10"、"10fp"。 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) | 是 | 文本最大显示字号。<br/>单位:fp | 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### enableHapticFeedback<sup>13+</sup> 760 761enableHapticFeedback(isEnabled: boolean) 762 763设置是否开启触控反馈。 764 765**原子化服务API:** 从API version 13开始,该接口支持在原子化服务中使用。 766 767**系统能力:** SystemCapability.ArkUI.ArkUI.Full 768 769**参数:** 770 771| 参数名 | 类型 | 必填 | 说明 | 772| ------ | ------- | ---- | ---------------------------------- | 773| isEnabled | boolean | 是 | 是否开启触控反馈。<br/>默认值:true | 774 775> **说明:** 776> 777> 开启触控反馈时,需要在工程的module.json5中配置requestPermissions字段开启振动权限,配置如下: 778> ```json 779> "requestPermissions": [ 780> { 781> "name": "ohos.permission.VIBRATE", 782> } 783> ] 784> ``` 785 786### keyboardAppearance<sup>15+</sup> 787 788keyboardAppearance(appearance: Optional\<KeyboardAppearance>) 789 790设置输入框拉起的键盘样式。 791 792**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。 793 794**系统能力:** SystemCapability.ArkUI.ArkUI.Full 795 796**参数:** 797 798| 参数名 | 类型 | 必填 | 说明 | 799| ------ | ----------------------------------------- | ---- | ------------------------------------------------------ | 800| appearance | [Optional](ts-universal-attributes-custom-property.md#optional12)\<[KeyboardAppearance](ts-text-common.md#keyboardappearance15枚举说明)> | 是 | 键盘样式。<br/>默认值:KeyboardAppearance.NONE_IMMERSIVE | 801 802### stopBackPress<sup>15+</sup> 803 804stopBackPress(isStopped: Optional\<boolean>) 805 806设置是否阻止返回键向其它组件或应用侧传递。 807 808**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。 809 810**系统能力:** SystemCapability.ArkUI.ArkUI.Full 811 812**参数:** 813 814| 参数名 | 类型 | 必填 | 说明 | 815| ------ | --------------------------------------------------- | ---- | ----------------------------------------- | 816| isStopped | [Optional](ts-universal-attributes-custom-property.md#optional12)\<boolean> | 是 | 是否消费返回键。 <br />默认值:true | 817 818## 事件 819 820除支持[通用事件](ts-component-general-events.md)外,还支持以下事件: 821 822### onChange 823 824onChange(callback: EditableTextOnChangeCallback) 825 826输入内容发生变化时,触发该回调。 827 828在本回调中,若执行了光标操作,需要开发者在预上屏场景下依据previewText参数调整光标逻辑,以适应预上屏场景。 829 830**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 831 832**系统能力:** SystemCapability.ArkUI.ArkUI.Full 833 834**参数:** 835 836| 参数名 | 类型 | 必填 | 说明 | 837| ------ | ------ | ---- | -------------------- | 838| callback | [EditableTextOnChangeCallback](ts-text-common.md#editabletextonchangecallback12) | 是 | 当前输入文本内容变化时的回调。 | 839 840### onEditChange<sup>10+</sup> 841 842onEditChange(callback: (isEditing: boolean) => void) 843 844输入状态变化时,触发该回调。有光标时为编辑态,无光标时为非编辑态。isEditing为true表示正在输入。 845 846**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 847 848**系统能力:** SystemCapability.ArkUI.ArkUI.Full 849 850**参数:** 851 852| 参数名 | 类型 | 必填 | 说明 | 853| --------- | ------- | ---- | -------------------- | 854| isEditing | boolean | 是 | 为true表示正在输入。 | 855 856### onCopy<sup>8+</sup> 857 858onCopy(callback: (value: string) => void) 859 860进行复制操作时,触发该回调。 861 862**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 863 864**系统能力:** SystemCapability.ArkUI.ArkUI.Full 865 866**参数:** 867 868| 参数名 | 类型 | 必填 | 说明 | 869| ------ | ------ | ---- | ---------------- | 870| value | string | 是 | 复制的文本内容。 | 871 872### onCut<sup>8+</sup> 873 874onCut(callback: (value: string) => void) 875 876进行剪切操作时,触发该回调。 877 878**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 879 880**系统能力:** SystemCapability.ArkUI.ArkUI.Full 881 882**参数:** 883 884| 参数名 | 类型 | 必填 | 说明 | 885| ------ | ------ | ---- | ---------------- | 886| value | string | 是 | 剪切的文本内容。 | 887 888### onPaste 889 890onPaste(callback: (value: string, event: PasteEvent) => void) 891 892进行粘贴操作时,触发该回调。 893 894**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 895 896**系统能力:** SystemCapability.ArkUI.ArkUI.Full 897 898**参数:** 899 900| 参数名 | 类型 | 必填 | 说明 | 901| ------------------- | ------------------------------------------------------------ | ---- | ---------------------- | 902| value | string | 是 | 粘贴的文本内容。 | 903| event<sup>11+</sup> | [PasteEvent](ts-basic-components-richeditor.md#pasteevent11) | 是 | 用户自定义的粘贴事件。 | 904 905### onTextSelectionChange<sup>10+</sup> 906 907onTextSelectionChange(callback: (selectionStart: number, selectionEnd: number) => void) 908 909文本选择的位置发生变化或编辑状态下光标位置发生变化时,触发该回调。 910 911**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 912 913**系统能力:** SystemCapability.ArkUI.ArkUI.Full 914 915**参数:** 916 917| 参数名 | 类型 | 必填 | 说明 | 918| -------------- | ------ | ---- | --------------------------------------- | 919| selectionStart | number | 是 | 所选文本的起始位置,文字的起始位置为0。 | 920| selectionEnd | number | 是 | 所选文本的结束位置。 | 921 922### onContentScroll<sup>10+</sup> 923 924onContentScroll(callback: (totalOffsetX: number, totalOffsetY: number) => void) 925 926文本内容滚动时,触发该回调。 927 928**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 929 930**系统能力:** SystemCapability.ArkUI.ArkUI.Full 931 932**参数:** 933 934| 参数名 | 类型 | 必填 | 说明 | 935| ------------ | ------ | ---- | ---------------------------------- | 936| totalOffsetX | number | 是 | 文本在内容区的横坐标偏移,单位px。 | 937| totalOffsetY | number | 是 | 文本在内容区的纵坐标偏移,单位px。 | 938 939### onSubmit<sup>11+</sup> 940 941onSubmit(callback: (enterKey: EnterKeyType) => void) 942 943按下软键盘输入法回车键触发该回调。 944 945**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 946 947**系统能力:** SystemCapability.ArkUI.ArkUI.Full 948 949**参数:** 950 951| 参数名 | 类型 | 必填 | 说明 | 952| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | 953| enterKey | [EnterKeyType](ts-basic-components-textinput.md#enterkeytype枚举说明) | 是 | 输入法回车键类型,类型为EnterKeyType.NEW_LINE时不触发onSubmit。 | 954 955### onSubmit<sup>14+</sup> 956 957onSubmit(callback: TextAreaSubmitCallback) 958 959按下软键盘输入法回车键触发该回调事件,提交事件时提供保持TextArea编辑状态的方法。 960 961**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 962 963**系统能力:** SystemCapability.ArkUI.ArkUI.Full 964 965**参数:** 966 967| 参数名 | 类型 | 必填 | 说明 | 968| ------ | ------- | ---- | ----------------------------- | 969| callback | [TextAreaSubmitCallback](#textareasubmitcallback14) | 是 | 按下软键盘输入法回车键时的回调事件。 | 970 971### onWillInsert<sup>12+</sup> 972 973onWillInsert(callback: Callback\<InsertValue, boolean>) 974 975在将要输入时,触发该回调。 976 977**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 978 979**系统能力:** SystemCapability.ArkUI.ArkUI.Full 980 981**参数:** 982 983| 参数名 | 类型 | 必填 | 说明 | 984| ------ | ------------------------------------------------------------ | ---- | ------------------ | 985| callback | Callback\<[InsertValue](ts-text-common.md#insertvalue12对象说明), boolean> | 是 | 在将要输入时调用的回调。<br/>在返回true时,表示正常插入,返回false时,表示不插入。<br/>在预上屏和候选词操作时,该回调不触发。<br/>仅支持系统输入法输入的场景。 | 986 987### onDidInsert<sup>12+</sup> 988 989onDidInsert(callback: Callback\<InsertValue>) 990 991在输入完成时,触发该回调。 992 993**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 994 995**系统能力:** SystemCapability.ArkUI.ArkUI.Full 996 997**参数:** 998 999| 参数名 | 类型 | 必填 | 说明 | 1000| ------ | ------------------------------------------------------------ | ---- | ------------------ | 1001| callback | Callback\<[InsertValue](ts-text-common.md#insertvalue12对象说明)> | 是 | 在输入完成时调用的回调。<br/>仅支持系统输入法输入的场景。 | 1002 1003### onWillDelete<sup>12+</sup> 1004 1005onWillDelete(callback: Callback\<DeleteValue, boolean>) 1006 1007在将要删除时,触发该回调。 1008 1009**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1010 1011**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1012 1013**参数:** 1014 1015| 参数名 | 类型 | 必填 | 说明 | 1016| ------ | ------------------------------------------------------------ | ---- | ------------------ | 1017| callback | Callback\<[DeleteValue](ts-text-common.md#deletevalue12对象说明), boolean> | 是 | 在将要删除时调用的回调。<br/>在返回true时,表示正常删除,返回false时,表示不删除。<br/>在预上屏删除操作时,该回调不触发。<br/>仅支持系统输入法输入的场景。 | 1018 1019### onDidDelete<sup>12+</sup> 1020 1021onDidDelete(callback: Callback\<DeleteValue>) 1022 1023在删除完成时,触发该回调。 1024 1025**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1026 1027**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1028 1029**参数:** 1030 1031| 参数名 | 类型 | 必填 | 说明 | 1032| ------ | ------------------------------------------------------------ | ---- | ------------------ | 1033| callback | Callback\<[DeleteValue](ts-text-common.md#deletevalue12对象说明)> | 是 | 在删除完成时调用的回调。<br/>仅支持系统输入法输入的场景。 | 1034 1035### onWillChange<sup>15+</sup> 1036 1037onWillChange(callback: Callback\<EditableTextChangeValue, boolean>) 1038 1039在文本内容将要发生变化时,触发该回调。 1040 1041onWillChange的回调时序晚于onWillInsert、onWillDelete,早于onDidInsert、onDidDelete。 1042 1043**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。 1044 1045**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1046 1047**参数:** 1048 1049| 参数名 | 类型 | 必填 | 说明 | 1050| ------ | ------------------------------------------------------------ | ---- | ------------------ | 1051| callback | Callback\<[EditableTextChangeValue](ts-text-common.md#editabletextchangevalue15), boolean> | 是 | 在文本内容将要发生变化时的回调。<br/>返回true时,表示正常修改。返回false时,表示拦截此次触发。 | 1052 1053## TextAreaController<sup>8+</sup> 1054 1055TextArea组件的控制器继承自[TextContentControllerBase](ts-types.md#textcontentcontrollerbase10)。 1056 1057**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1058 1059**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1060 1061### 导入对象 1062 1063``` 1064controller: TextAreaController = new TextAreaController() 1065``` 1066 1067### constructor<sup>8+</sup> 1068 1069constructor() 1070 1071TextAreaController的构造函数。 1072 1073**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1074 1075**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1076 1077### caretPosition<sup>8+</sup> 1078 1079caretPosition(value: number): void 1080 1081设置输入光标的位置。 1082 1083**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1084 1085**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1086 1087**参数:** 1088 1089| 参数名 | 类型 | 必填 | 说明 | 1090| ------ | ---- | ---- | ---- | 1091| value | number | 是 | 从字符串开始到光标所在位置的字符长度。 | 1092 1093### setTextSelection<sup>10+</sup> 1094 1095setTextSelection(selectionStart: number, selectionEnd: number, options?: SelectionOptions): void 1096 1097组件在获焦状态下,调用该接口设置文本选择区域并高亮显示,且只有在selectionStart小于selectionEnd时,文字才会被选取、高亮显示。 1098 1099**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1100 1101**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1102 1103**参数:** 1104 1105| 参数名 | 类型 | 必填 | 说明 | 1106| ------ | ---- | ---- | ---- | 1107| selectionStart | number | 是 | 文本选择区域起始位置,文本框中文字的起始位置为0。<br/>当selectionStart小于0时、按照0处理;当selectionStart大于文字最大长度时、按照文字最大长度处理。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1108| selectionEnd | number | 是 | 文本选择区域结束位置。<br/>当selectionEnd小于0时、按照0处理;当selectionEnd大于文字最大长度时、按照文字最大长度处理。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1109| options<sup>12+</sup> | [SelectionOptions](ts-types.md#selectionoptions12对象说明) | 否 | 选中文字时的配置。<br />默认值:MenuPolicy.DEFAULT<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1110 1111> **说明:** 1112> 1113> 如果selectionMenuHidden被赋值为true或设备为2in1时,即使options被赋值为MenuPolicy.SHOW,调用setTextSelection也不弹出菜单。 1114> 1115> 如果选中的文本含有emoji表情时,表情的起始位置包含在设置的文本选中区域内就会被选中。 1116 1117### stopEditing<sup>10+</sup> 1118 1119stopEditing(): void 1120 1121退出编辑态。 1122 1123**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1124 1125**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1126 1127## TextAreaType<sup>11+</sup>枚举说明 1128 1129**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1130 1131**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1132 1133| 名称 | 值 | 说明 | 1134| ------ | ----- | ------ | 1135| NORMAL | 0 | 基本输入模式。<br/>支持输入数字、字母、下划线、空格、特殊字符。 | 1136| NUMBER | 2 | 纯数字输入模式。 | 1137| PHONE_NUMBER | 3 | 电话号码输入模式。<br/>支持输入数字、空格、+ 、-、*、#、(、),长度不限。 | 1138| EMAIL | 5 | 邮箱地址输入模式。<br/>支持数字,字母,下划线、小数点、!、#、$、%、&、'、*、+、-、/、=、?、^、`、\{、\|、\}、~,以及@字符(只能存在一个@字符)。 | 1139| NUMBER_DECIMAL<sup>12+</sup> | 12 | 带小数点的数字输入模式。<br/>支持数字,小数点(只能存在一个小数点)。| 1140| URL<sup>12+</sup> | 13 | 带URL的输入模式。 | 1141 1142## ContentType<sup>12+</sup>枚举说明 1143 1144自动填充类型。 1145 1146**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1147 1148**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1149 1150| 名称 | 值 | 说明 | 1151| -------------------------- | ---- | ------------------------------------------------------------ | 1152| USER_NAME | 0 | 【用户名】在已启用密码保险箱的情况下,支持用户名的自动保存和自动填充。 | 1153| PASSWORD | 1 | 【密码】在已启用密码保险箱的情况下,支持密码的自动保存和自动填充。 | 1154| NEW_PASSWORD | 2 | 【新密码】在已启用密码保险箱的情况下,支持自动生成新密码。 | 1155| FULL_STREET_ADDRESS | 3 | 【详细地址】在已启用情景化自动填充的情况下,支持详细地址的自动保存和自动填充。 | 1156| HOUSE_NUMBER | 4 | 【门牌号】在已启用情景化自动填充的情况下,支持门牌号的自动保存和自动填充。 | 1157| DISTRICT_ADDRESS | 5 | 【区/县】在已启用情景化自动填充的情况下,支持区/县的自动保存和自动填充。 | 1158| CITY_ADDRESS | 6 | 【市】在已启用情景化自动填充的情况下,支持市的自动保存和自动填充。 | 1159| PROVINCE_ADDRESS | 7 | 【省】在已启用情景化自动填充的情况下,支持省的自动保存和自动填充。 | 1160| COUNTRY_ADDRESS | 8 | 【国家】在已启用情景化自动填充的情况下,支持国家的自动保存和自动填充。 | 1161| PERSON_FULL_NAME | 9 | 【姓名】在已启用情景化自动填充的情况下,支持姓名的自动保存和自动填充。 | 1162| PERSON_LAST_NAME | 10 | 【姓氏】在已启用情景化自动填充的情况下,支持姓氏的自动保存和自动填充。 | 1163| PERSON_FIRST_NAME | 11 | 【名字】在已启用情景化自动填充的情况下,支持名字的自动保存和自动填充。 | 1164| PHONE_NUMBER | 12 | 【手机号码】在已启用情景化自动填充的情况下,支持手机号码的自动保存和自动填充。 | 1165| PHONE_COUNTRY_CODE | 13 | 【国家代码】在已启用情景化自动填充的情况下,支持国家代码的自动保存和自动填充。 | 1166| FULL_PHONE_NUMBER | 14 | 【包含国家代码的手机号码】在已启用情景化自动填充的情况下,支持包含国家代码的手机号码的自动保存和自动填充。 | 1167| EMAIL_ADDRESS | 15 | 【邮箱地址】在已启用情景化自动填充的情况下,支持邮箱地址的自动保存和自动填充。 | 1168| BANK_CARD_NUMBER | 16 | 【银行卡号】在已启用情景化自动填充的情况下,支持银行卡号的自动保存和自动填充。 | 1169| ID_CARD_NUMBER | 17 | 【身份证号】在已启用情景化自动填充的情况下,支持身份证号的自动保存和自动填充。 | 1170| NICKNAME | 23 | 【昵称】在已启用情景化自动填充的情况下,支持昵称的自动保存和自动填充。 | 1171| DETAIL_INFO_WITHOUT_STREET | 24 | 【无街道地址】在已启用情景化自动填充的情况下,支持无街道地址的自动保存和自动填充。 | 1172| FORMAT_ADDRESS | 25 | 【标准地址】在已启用情景化自动填充的情况下,支持标准地址的自动保存和自动填充。 | 1173 1174## TextAreaSubmitCallback<sup>14+</sup> 1175 1176type TextAreaSubmitCallback = (enterKeyType: EnterKeyType, event?: SubmitEvent) => void 1177 1178软键盘按下回车键时的回调事件。 1179 1180**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 1181 1182**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1183 1184**参数:** 1185 1186| 参数名 | 类型 | 必填 | 说明 | 1187| -------- | ------------------------------------------------------------ | ---- | -------------------------------------------------------- | 1188| enterKeyType | [EnterKeyType](ts-basic-components-textinput.md#enterkeytype枚举说明) | 是 | 软键盘输入法回车键类型。 | 1189| event | [SubmitEvent](ts-basic-components-textinput.md#submitevent11) | 否 | 提交事件。 | 1190 1191## 示例 1192 1193### 示例1(设置与获取光标位置) 1194 1195该示例通过controller实现了光标位置的设置与获取。 1196 1197```ts 1198// xxx.ets 1199@Entry 1200@Component 1201struct TextAreaExample { 1202 @State text: string = '' 1203 @State positionInfo: CaretOffset = { index: 0, x: 0, y: 0 } 1204 controller: TextAreaController = new TextAreaController() 1205 1206 build() { 1207 Column() { 1208 TextArea({ 1209 text: this.text, 1210 placeholder: 'The text area can hold an unlimited amount of text. input your word...', 1211 controller: this.controller 1212 }) 1213 .placeholderFont({ size: 16, weight: 400 }) 1214 .width(336) 1215 .height(56) 1216 .margin(20) 1217 .fontSize(16) 1218 .fontColor('#182431') 1219 .backgroundColor('#FFFFFF') 1220 .onChange((value: string) => { 1221 this.text = value 1222 }) 1223 Text(this.text) 1224 Button('Set caretPosition 1') 1225 .backgroundColor('#007DFF') 1226 .margin(15) 1227 .onClick(() => { 1228 // 设置光标位置到第一个字符后 1229 this.controller.caretPosition(1) 1230 }) 1231 Button('Get CaretOffset') 1232 .backgroundColor('#007DFF') 1233 .margin(15) 1234 .onClick(() => { 1235 this.positionInfo = this.controller.getCaretOffset() 1236 }) 1237 }.width('100%').height('100%').backgroundColor('#F1F3F5') 1238 } 1239} 1240``` 1241 1242 1243 1244### 示例2(设置计数器) 1245 1246该示例通过maxLength、showCounter属性实现了计数器的功能。 1247 1248```ts 1249// xxx.ets 1250@Entry 1251@Component 1252struct TextAreaExample { 1253 @State text: string = '' 1254 controller: TextAreaController = new TextAreaController() 1255 1256 build() { 1257 Column() { 1258 TextArea({ 1259 text: this.text, 1260 placeholder: 'The text area can hold an unlimited amount of text. input your word...', 1261 controller: this.controller 1262 }) 1263 .placeholderFont({ size: 16, weight: 400 }) 1264 .width(336) 1265 .height(56) 1266 .margin(20) 1267 .fontSize(16) 1268 .fontColor('#182431') 1269 .backgroundColor('#FFFFFF') 1270 .maxLength(4) 1271 .showCounter(true, { thresholdPercentage: 50, highlightBorder: true }) 1272 //计数器显示效果为用户当前输入字符数/最大字符限制数。最大字符限制数通过maxLength()接口设置。 1273 //如果用户当前输入字符数达到最大字符限制乘50%(thresholdPercentage)。字符计数器显示。 1274 //用户设置highlightBorder为false时,配置取消红色边框。不设置此参数时,默认为true。 1275 .onChange((value: string) => { 1276 this.text = value 1277 }) 1278 }.width('100%').height('100%').backgroundColor('#F1F3F5') 1279 } 1280} 1281``` 1282 1283 1284 1285 1286### 示例3(设置自定义键盘) 1287 1288该示例通过customKeyboard属性实现了自定义键盘的功能。 1289 1290```ts 1291// xxx.ets 1292@Entry 1293@Component 1294struct TextAreaExample { 1295 controller: TextAreaController = new TextAreaController() 1296 @State inputValue: string = "" 1297 1298 // 自定义键盘组件 1299 @Builder CustomKeyboardBuilder() { 1300 Column() { 1301 Button('x').onClick(() => { 1302 // 关闭自定义键盘 1303 this.controller.stopEditing() 1304 }) 1305 Grid() { 1306 ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'], (item: number | string) => { 1307 GridItem() { 1308 Button(item + "") 1309 .width(110).onClick(() => { 1310 this.inputValue += item 1311 }) 1312 } 1313 }) 1314 }.maxCount(3).columnsGap(10).rowsGap(10).padding(5) 1315 }.backgroundColor(Color.Gray) 1316 } 1317 1318 build() { 1319 Column() { 1320 TextArea({ controller: this.controller, text: this.inputValue}) 1321 // 绑定自定义键盘 1322 .customKeyboard(this.CustomKeyboardBuilder()).margin(10).border({ width: 1 }) 1323 .height(200) 1324 } 1325 } 1326} 1327``` 1328 1329 1330 1331### 示例4(设置输入法回车键类型) 1332 1333该示例通过enterKeyType属性实现了动态切换输入法回车键的效果。 1334 1335```ts 1336// xxx.ets 1337@Entry 1338@Component 1339struct TextAreaExample { 1340 @State text: string = '' 1341 @State enterTypes: Array<EnterKeyType> = 1342 [EnterKeyType.Go, EnterKeyType.Search, EnterKeyType.Send, EnterKeyType.Done, EnterKeyType.Next, 1343 EnterKeyType.PREVIOUS, EnterKeyType.NEW_LINE] 1344 @State index: number = 0 1345 1346 build() { 1347 Column({ space: 20 }) { 1348 TextArea({ placeholder: '请输入用户名', text: this.text }) 1349 .width(380) 1350 .enterKeyType(this.enterTypes[this.index]) 1351 .onChange((value: string) => { 1352 this.text = value 1353 }) 1354 .onSubmit((enterKey: EnterKeyType) => { 1355 console.log("trigger area onsubmit" + enterKey); 1356 }) 1357 Button('改变EnterKeyType').onClick(() => { 1358 this.index = (this.index + 1) % this.enterTypes.length; 1359 }) 1360 1361 }.width('100%') 1362 } 1363} 1364``` 1365 1366 1367 1368 1369### 示例5(设置文本断行规则) 1370 1371该示例通过wordBreak属性实现了TextArea不同断行规则下的效果。 1372 1373```ts 1374// xxx.ets 1375@Entry 1376@Component 1377struct TextAreaExample { 1378 build() { 1379 Column() { 1380 Text("属性WordBreakType为NORMAL的样式:").fontSize(16).fontColor(0xFF0000) 1381 TextArea({ 1382 text: 'This is set wordBreak to WordBreak text Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu.' 1383 }) 1384 .fontSize(16) 1385 .border({ width: 1 }) 1386 .wordBreak(WordBreak.NORMAL) 1387 Text("英文文本,属性WordBreakType为BREAK_ALL的样式:").fontSize(16).fontColor(0xFF0000) 1388 TextArea({ 1389 text: 'This is set wordBreak to WordBreak text Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu.' 1390 }) 1391 .fontSize(16) 1392 .border({ width: 1 }) 1393 .wordBreak(WordBreak.BREAK_ALL) 1394 Text("中文文本,属性WordBreakType为BREAK_ALL的样式:").fontSize(16).fontColor(0xFF0000) 1395 TextArea({ 1396 text: '多行文本输入框组件,当输入的文本内容超过组件宽度时会自动换行显示。\n高度未设置时,组件无默认高度,自适应内容高度。宽度未设置时,默认撑满最大宽度。' 1397 }) 1398 .fontSize(16) 1399 .border({ width: 1 }) 1400 .wordBreak(WordBreak.BREAK_ALL) 1401 Text("属性WordBreakType为BREAK_WORD的样式:").fontSize(16).fontColor(0xFF0000) 1402 TextArea({ 1403 text: 'This is set wordBreak to WordBreak text Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu.' 1404 }) 1405 .fontSize(16) 1406 .border({ width: 1 }) 1407 .wordBreak(WordBreak.BREAK_WORD) 1408 } 1409 } 1410} 1411``` 1412 1413 1414 1415### 示例6(设置文本样式) 1416 1417该示例通过lineHeight、letterSpacing、decoration属性展示了不同样式的文本效果。 1418 1419```ts 1420// xxx.ets 1421@Entry 1422@Component 1423struct TextAreaExample { 1424 build() { 1425 Row() { 1426 Column() { 1427 Text('lineHeight').fontSize(9).fontColor(0xCCCCCC) 1428 TextArea({text: 'lineHeight unset'}) 1429 .border({ width: 1 }).padding(10).margin(5) 1430 TextArea({text: 'lineHeight 15'}) 1431 .border({ width: 1 }).padding(10).margin(5).lineHeight(15) 1432 TextArea({text: 'lineHeight 30'}) 1433 .border({ width: 1 }).padding(10).margin(5).lineHeight(30) 1434 1435 Text('letterSpacing').fontSize(9).fontColor(0xCCCCCC) 1436 TextArea({text: 'letterSpacing 0'}) 1437 .border({ width: 1 }).padding(5).margin(5).letterSpacing(0) 1438 TextArea({text: 'letterSpacing 3'}) 1439 .border({ width: 1 }).padding(5).margin(5).letterSpacing(3) 1440 TextArea({text: 'letterSpacing -1'}) 1441 .border({ width: 1 }).padding(5).margin(5).letterSpacing(-1) 1442 1443 Text('decoration').fontSize(9).fontColor(0xCCCCCC) 1444 TextArea({text: 'LineThrough, Red\nsecond line'}) 1445 .border({ width: 1 }).padding(5).margin(5) 1446 .decoration({type: TextDecorationType.LineThrough, color: Color.Red}) 1447 TextArea({text: 'Overline, Red, DOTTED\nsecond line'}) 1448 .border({ width: 1 }).padding(5).margin(5) 1449 .decoration({type: TextDecorationType.Overline, color: Color.Red, style: TextDecorationStyle.DOTTED}) 1450 TextArea({text: 'Underline, Red, WAVY\nsecond line'}) 1451 .border({ width: 1 }).padding(5).margin(5) 1452 .decoration({type: TextDecorationType.Underline, color: Color.Red, style: TextDecorationStyle.WAVY}) 1453 }.height('90%') 1454 } 1455 .width('90%') 1456 .margin(10) 1457 } 1458} 1459``` 1460 1461 1462 1463### 示例7(设置文字特性效果) 1464 1465该示例通过fontFeature属性实现了文本在不同文字特性下的展示效果。 1466 1467```ts 1468// xxx.ets 1469@Entry 1470@Component 1471struct TextAreaExample { 1472 @State text1: string = 'This is ss01 on : 0123456789' 1473 @State text2: string = 'This is ss01 off: 0123456789' 1474 1475 build() { 1476 Column(){ 1477 TextArea({text: this.text1}) 1478 .fontSize(20) 1479 .margin({top:200}) 1480 .fontFeature("\"ss01\" on") 1481 TextArea({text : this.text2}) 1482 .margin({top:10}) 1483 .fontSize(20) 1484 .fontFeature("\"ss01\" off") 1485 } 1486 .width("90%") 1487 .margin("5%") 1488 } 1489} 1490``` 1491 1492 1493### 示例8(自定义键盘避让) 1494 1495该示例通过自定义键盘实现了键盘避让的功能。 1496 1497```ts 1498// xxx.ets 1499@Entry 1500@Component 1501struct TextAreaExample { 1502 controller: TextAreaController = new TextAreaController() 1503 @State inputValue: string = "" 1504 @State height1:string|number = '80%' 1505 @State height2:number = 100 1506 @State supportAvoidance:boolean = true; 1507 1508 // 自定义键盘组件 1509 @Builder CustomKeyboardBuilder() { 1510 Column() { 1511 Row(){ 1512 Button('x').onClick(() => { 1513 // 关闭自定义键盘 1514 this.controller.stopEditing() 1515 }).margin(10) 1516 } 1517 Grid() { 1518 ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'], (item: number | string) => { 1519 GridItem() { 1520 Button(item + "") 1521 .width(110).onClick(() => { 1522 this.inputValue += item 1523 }) 1524 } 1525 }) 1526 }.maxCount(3).columnsGap(10).rowsGap(10).padding(5) 1527 }.backgroundColor(Color.Gray) 1528 } 1529 1530 build() { 1531 Column() { 1532 Row(){ 1533 Button("20%") 1534 .fontSize(24) 1535 .onClick(()=>{ 1536 this.height1 = "20%" 1537 }) 1538 Button("80%") 1539 .fontSize(24) 1540 .margin({left:20}) 1541 .onClick(()=>{ 1542 this.height1 = "80%" 1543 }) 1544 } 1545 .justifyContent(FlexAlign.Center) 1546 .alignItems(VerticalAlign.Bottom) 1547 .height(this.height1) 1548 .width("100%") 1549 .padding({bottom:50}) 1550 TextArea({ controller: this.controller, text: this.inputValue}) 1551 .height(100) 1552 // 绑定自定义键盘 1553 .customKeyboard(this.CustomKeyboardBuilder(),{ supportAvoidance: this.supportAvoidance }).margin(10).border({ width: 1 }) 1554 // .height(200) 1555 } 1556 } 1557} 1558``` 1559 1560 1561### 示例9(设置文本自适应) 1562 1563该示例通过minFontSize、maxFontSize、heightAdaptivePolicy属性展示了文本自适应字号的效果。 1564 1565```ts 1566// xxx.ets 1567@Entry 1568@Component 1569struct TextAreaExample { 1570 build() { 1571 Row() { 1572 Column() { 1573 Text('heightAdaptivePolicy').fontSize(9).fontColor(0xCCCCCC) 1574 TextArea({text: 'This is the text with the height adaptive policy set'}) 1575 .width('80%').height(90).borderWidth(1).margin(1) 1576 .minFontSize(4) 1577 .maxFontSize(40) 1578 .maxLines(3) 1579 .heightAdaptivePolicy(TextHeightAdaptivePolicy.MAX_LINES_FIRST) 1580 TextArea({text: 'This is the text with the height adaptive policy set'}) 1581 .width('80%').height(90).borderWidth(1).margin(1) 1582 .minFontSize(4) 1583 .maxFontSize(40) 1584 .maxLines(3) 1585 .heightAdaptivePolicy(TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST) 1586 TextArea({text: 'This is the text with the height adaptive policy set'}) 1587 .width('80%').height(90).borderWidth(1).margin(1) 1588 .minFontSize(4) 1589 .maxFontSize(40) 1590 .maxLines(3) 1591 .heightAdaptivePolicy(TextHeightAdaptivePolicy.LAYOUT_CONSTRAINT_FIRST) 1592 }.height('90%') 1593 } 1594 .width('90%') 1595 .margin(10) 1596 } 1597} 1598``` 1599 1600 1601 1602### 示例10(设置文本行间距) 1603 1604该示例通过lineSpacing属性展示了文本在不同行间距下的展示效果。 1605 1606```ts 1607// xxx.ets 1608import { LengthMetrics } from '@kit.ArkUI' 1609 1610@Entry 1611@Component 1612struct TextAreaExample { 1613 build() { 1614 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) { 1615 Text('TextArea lineSpacing.').fontSize(9).fontColor(0xCCCCCC) 1616 TextArea({ placeholder: 'This is the TextArea with no lineSpacing set.' }) 1617 .fontSize(12) 1618 TextArea({ placeholder: 'This is the TextArea with lineSpacing set to 20_px.' }) 1619 .fontSize(12) 1620 .lineSpacing(LengthMetrics.px(20)) 1621 TextArea({ placeholder: 'This is the TextArea with lineSpacing set to 20_vp.' }) 1622 .fontSize(12) 1623 .lineSpacing(LengthMetrics.vp(20)) 1624 TextArea({ placeholder: 'This is the TextArea with lineSpacing set to 20_fp.' }) 1625 .fontSize(12) 1626 .lineSpacing(LengthMetrics.fp(20)) 1627 TextArea({ placeholder: 'This is the TextArea with lineSpacing set to 20_lpx.' }) 1628 .fontSize(12) 1629 .lineSpacing(LengthMetrics.lpx(20)) 1630 TextArea({ placeholder: 'This is the TextArea with lineSpacing set to 100%.' }) 1631 .fontSize(12) 1632 .lineSpacing(LengthMetrics.percent(1)) 1633 }.height(600).width(350).padding({ left: 35, right: 35, top: 35 }) 1634 } 1635} 1636``` 1637 1638 1639 1640### 示例11(设置自动填充) 1641 1642该示例通过contentType、enableAutoFill属性实现了文本自动填充的功能。 1643 1644```ts 1645// xxx.ets 1646@Entry 1647@Component 1648struct TextAreaExample { 1649 @State text: string = '' 1650 1651 build() { 1652 Column() { 1653 // 邮箱地址自动填充类型 1654 TextArea({ placeholder: 'input your email...' }) 1655 .width('95%') 1656 .height(40) 1657 .margin(20) 1658 .contentType(ContentType.EMAIL_ADDRESS) 1659 .enableAutoFill(true) 1660 .maxLength(20) 1661 // 街道地址自动填充类型 1662 TextArea({ placeholder: 'input your street address...' }) 1663 .width('95%') 1664 .height(40) 1665 .margin(20) 1666 .contentType(ContentType.FULL_STREET_ADDRESS) 1667 .enableAutoFill(true) 1668 .maxLength(20) 1669 }.width('100%').height('100%').backgroundColor('#F1F3F5') 1670 } 1671} 1672``` 1673 1674 1675 1676### 示例12(设置折行规则) 1677 1678该示例通过lineBreakStrategy属性实现了TextArea不同折行规则下的效果。 1679 1680```ts 1681// xxx.ets 1682@Entry 1683@Component 1684struct TextAreaExample { 1685 @State message1: string = 1686 "They can be classified as built-in components–those directly provided by the ArkUI framework and custom components – those defined by developers" + 1687 "The built-in components include buttons radio buttonsprogress indicators and text You can set the rendering effectof thesecomponents in method chaining mode," + 1688 "page components are divided into independent UI units to implementindependent creation development and reuse of different units on pages making pages more engineering-oriented." 1689 @State lineBreakStrategyIndex: number = 0 1690 @State lineBreakStrategy: LineBreakStrategy[] = 1691 [LineBreakStrategy.GREEDY, LineBreakStrategy.HIGH_QUALITY, LineBreakStrategy.BALANCED] 1692 @State lineBreakStrategyStr: string[] = ['GREEDY', 'HIGH_QUALITY', 'BALANCED'] 1693 1694 build() { 1695 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start }) { 1696 Text('lineBreakStrategy').fontSize(9).fontColor(0xCCCCCC) 1697 TextArea({ text: this.message1 }) 1698 .fontSize(12) 1699 .border({ width: 1 }) 1700 .padding(10) 1701 .width('100%') 1702 .lineBreakStrategy(this.lineBreakStrategy[this.lineBreakStrategyIndex]) 1703 Row() { 1704 Button('当前lineBreakStrategy模式:' + this.lineBreakStrategyStr[this.lineBreakStrategyIndex]).onClick(() => { 1705 this.lineBreakStrategyIndex++ 1706 if (this.lineBreakStrategyIndex > (this.lineBreakStrategyStr.length - 1)) { 1707 this.lineBreakStrategyIndex = 0 1708 } 1709 }) 1710 }.padding({ top: 10 }) 1711 }.height(700).width(370).padding({ left: 35, right: 35, top: 35 }) 1712 } 1713} 1714``` 1715 1716 1717 1718### 示例13(支持插入和删除回调) 1719 1720该示例通过onWillInsert、onDidInsert、onWillDelete、onDidDelete接口实现了插入和删除的功能。 1721 1722```ts 1723// xxx.ets 1724@Entry 1725@Component 1726struct TextAreaExample { 1727 @State insertValue: string = "" 1728 @State deleteValue: string = "" 1729 @State insertOffset: number = 0 1730 @State deleteOffset: number = 0 1731 @State deleteDirection: number = 0 1732 1733 build() { 1734 Row() { 1735 Column() { 1736 TextArea({ text: "TextArea支持插入回调文本" }) 1737 .width(300) 1738 .height(60) 1739 .onWillInsert((info: InsertValue) => { 1740 this.insertValue = info.insertValue 1741 return true; 1742 }) 1743 .onDidInsert((info: InsertValue) => { 1744 this.insertOffset = info.insertOffset 1745 }) 1746 1747 Text("insertValue:" + this.insertValue + " insertOffset:" + this.insertOffset).height(30) 1748 1749 TextArea({ text: "TextArea支持删除回调文本b" }) 1750 .width(300) 1751 .height(60) 1752 .onWillDelete((info: DeleteValue) => { 1753 this.deleteValue = info.deleteValue 1754 info.direction 1755 return true; 1756 }) 1757 .onDidDelete((info: DeleteValue) => { 1758 this.deleteOffset = info.deleteOffset 1759 this.deleteDirection = info.direction 1760 }) 1761 1762 Text("deleteValue:" + this.deleteValue + " deleteOffset:" + this.deleteOffset).height(30) 1763 Text("deleteDirection:" + (this.deleteDirection == 0 ? "BACKWARD" : "FORWARD")).height(30) 1764 1765 }.width('100%') 1766 } 1767 .height('100%') 1768 } 1769} 1770``` 1771 1772 1773 1774### 示例14(文本扩展自定义菜单) 1775 1776该示例通过editMenuOptions接口实现了文本设置自定义菜单扩展项的文本内容、图标以及回调的功能。 1777 1778```ts 1779// xxx.ets 1780@Entry 1781@Component 1782struct TextAreaExample { 1783 @State text: string = 'TextArea editMenuOptions' 1784 onCreateMenu = (menuItems: Array<TextMenuItem>) => { 1785 let item1: TextMenuItem = { 1786 content: 'custom1', 1787 icon: $r('app.media.startIcon'), 1788 id: TextMenuItemId.of('custom1'), 1789 } 1790 let item2: TextMenuItem = { 1791 content: 'custom2', 1792 id: TextMenuItemId.of('custom2'), 1793 icon: $r('app.media.startIcon'), 1794 } 1795 menuItems.push(item1) 1796 menuItems.unshift(item2) 1797 return menuItems 1798 } 1799 onMenuItemClick = (menuItem: TextMenuItem, textRange: TextRange) => { 1800 if (menuItem.id.equals(TextMenuItemId.of("custom2"))) { 1801 console.log("拦截 id: custom2 start:" + textRange.start + "; end:" + textRange.end) 1802 return true 1803 } 1804 if (menuItem.id.equals(TextMenuItemId.COPY)) { 1805 console.log("拦截 COPY start:" + textRange.start + "; end:" + textRange.end) 1806 return true 1807 } 1808 if (menuItem.id.equals(TextMenuItemId.SELECT_ALL)) { 1809 console.log("不拦截 SELECT_ALL start:" + textRange.start + "; end:" + textRange.end) 1810 return false 1811 } 1812 return false 1813 } 1814 @State editMenuOptions: EditMenuOptions = { 1815 onCreateMenu: this.onCreateMenu, onMenuItemClick: this.onMenuItemClick 1816 } 1817 1818 build() { 1819 Column() { 1820 TextArea({ text: this.text }) 1821 .width('95%') 1822 .height(56) 1823 .editMenuOptions(this.editMenuOptions) 1824 .margin({ top: 100 }) 1825 } 1826 .width("90%") 1827 .margin("5%") 1828 } 1829} 1830``` 1831 1832 1833 1834