• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# TextArea
2
3The **\<TextArea>** component provides multi-line text input and automatically wraps text so that each line does not have more than the width of the component.
4
5>  **NOTE**
6>
7>  This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
8
9
10## Child Components
11
12Not supported
13
14
15## APIs
16
17TextArea(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextAreaController})
18
19**Parameters**
20
21| Name                    | Type                                    | Mandatory  | Description          |
22| ----------------------- | ---------------------------------------- | ---- | -------------- |
23| placeholder      | [ResourceStr](ts-types.md#resourcestr)  | No   | Placeholder text displayed when there is no input. It is not displayed once there is any input.    |
24| text             | [ResourceStr](ts-types.md#resourcestr)  | No   | Current text input.<br>If the component has [stateStyles](ts-universal-attributes-polymorphic-style.md) or any other attribute that may trigger updating configured, you are advised to bind the state variable to the text in real time through the **onChange** event,<br>so as to prevent display errors when the component is updated.    |
25| controller<sup>8+</sup> | [TextAreaController](#textareacontroller8) | No   | Text area controller.|
26
27
28## Attributes
29
30In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
31
32| Name                    | Type                                                    | Description                                                        |
33| ------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
34| placeholderColor         | [ResourceColor](ts-types.md#resourcecolor)                   | Placeholder text color.                                   |
35| placeholderFont          | [Font](ts-types.md#font)                                     | Placeholder text style, including the font size, font width, font family, and font style. Currently, only the default font family is supported.|
36| textAlign                | [TextAlign](ts-appendix-enums.md#textalign)                  | Horizontal alignment of the text.<br>Default value: **TextAlign.Start**|
37| caretColor               | [ResourceColor](ts-types.md#resourcecolor)                   | Color of the caret in the text box.                                        |
38| inputFilter<sup>8+</sup> | {<br>value: [ResourceStr](ts-types.md#resourcestr),<br>error?: (value: string) => void<br>} | Regular expression for input filtering. Only inputs that comply with the regular expression can be displayed. Other inputs are filtered out. The specified regular expression can match single characters, but not strings.<br>- **value**: regular expression to set.<br>- **error**: filtered-out content to return when regular expression matching fails.|
39| copyOption<sup>9+</sup>  | [CopyOptions](ts-appendix-enums.md#copyoptions9)             | Whether copy and paste is allowed.<br>If this attribute is set to **CopyOptions.None**, the paste operation is allowed, but not the copy or cut operation.|
40
41>  **NOTE**
42>
43>  The default value of the universal attribute [padding](ts-universal-attributes-size.md) is as follows: <br>{<br> top: 8 vp,<br> right: 16 vp,<br> bottom: 8 vp,<br> left: 16 vp<br> }
44
45
46## Events
47
48In addition to the [universal events](ts-universal-events-click.md), the following events are supported.
49
50| Name                                                         | Description                                                  |
51| ------------------------------------------------------------ | ------------------------------------------------------------ |
52| onChange(callback: (value: string) =&gt; void)               | Triggered when the input in the text box changes.<br>- **value**: text entered. |
53| onCopy<sup>8+</sup>(callback:(value: string) =&gt; void)     | Triggered when the copy button on the pasteboard, which displays when the text box is long pressed, is clicked.<br>- **value**: text to be copied. |
54| onCut<sup>8+</sup>(callback:(value: string) =&gt; void)      | Triggered when the cut button on the pasteboard, which displays when the text box is long pressed, is clicked.<br>- **value**: text to be cut. |
55| onPaste<sup>8+</sup>(callback:(value: string) =&gt; void)    | Triggered when the paste button on the pasteboard, which displays when the text box is long pressed, is clicked.<br>- **value**: text to be pasted. |
56
57## TextAreaController<sup>8+</sup>
58
59Defines the controller for controlling the **\<TextArea>** component. Currently, the controller can be used to control the caret position.
60
61### Objects to Import
62
63```
64controller: TextAreaController = new TextAreaController()
65```
66
67### caretPosition<sup>8+</sup>
68
69caretPosition(value: number): void
70
71Sets the position of the caret.
72
73**Parameters**
74
75| Name| Type| Mandatory| Description                              |
76| ------ | -------- | ---- | -------------------------------------- |
77| value  | number   | Yes  | Length from the start of the string to the position where the caret is located.|
78
79## Example
80
81```ts
82// xxx.ets
83@Entry
84@Component
85struct TextAreaExample {
86  @State text: string = ''
87  controller: TextAreaController = new TextAreaController()
88
89  build() {
90    Column() {
91      TextArea({
92        text: this.text,
93        placeholder: 'The text area can hold an unlimited amount of text. input your word...',
94        controller: this.controller
95      })
96        .placeholderFont({ size: 16, weight: 400 })
97        .width(336)
98        .height(56)
99        .margin(20)
100        .fontSize(16)
101        .fontColor('#182431')
102        .backgroundColor('#FFFFFF')
103        .onChange((value: string) => {
104          this.text = value
105        })
106      Text(this.text)
107      Button('Set caretPosition 1')
108        .backgroundColor('#007DFF')
109        .margin(15)
110        .onClick(() => {
111          // Move the caret to after the first entered character.
112          this.controller.caretPosition(1)
113        })
114    }.width('100%').height('100%').backgroundColor('#F1F3F5')
115  }
116}
117```
118
119![textArea](figures/textArea.gif)
120