ts-basic-components-textarea.md 5.6 KB
Newer Older
Z
zengyawen 已提交
1
# TextArea
Z
zengyawen 已提交
2

E
esterzhou 已提交
3
The **\<TextArea>** component provides multi-line text input and responds to certain input events.
Z
zengyawen 已提交
4

E
esterzhou 已提交
5
>  **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
7
>  This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9

E
esterzhou 已提交
10
## Child Components
Z
zengyawen 已提交
11

E
ester.zhou 已提交
12
Not supported
Z
zengyawen 已提交
13

Z
zengyawen 已提交
14 15 16

## APIs

E
esterzhou 已提交
17
TextArea(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextAreaController})
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19
**Parameters**
Z
zengyawen 已提交
20

E
ester.zhou 已提交
21 22 23 24 25
| Name                    | Type                                    | Mandatory  | Description          |
| ----------------------- | ---------------------------------------- | ---- | -------------- |
| placeholder      | [ResourceStr](ts-types.md#resourcestr)  | No   | Text displayed when there is no input.    |
| text             | [ResourceStr](ts-types.md#resourcestr)  | No   | Current text input.    |
| controller<sup>8+</sup> | [TextAreaController](#textareacontroller8) | No   | Text area controller.|
Z
zengyawen 已提交
26 27


E
ester.zhou 已提交
28
## Attributes
Z
zengyawen 已提交
29

E
ester.zhou 已提交
30
In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
Z
zengyawen 已提交
31

E
ester.zhou 已提交
32 33 34 35 36 37 38 39
| Name                      | Type                                    | Description                                      |
| ------------------------ | ---------------------------------------- | ---------------------------------------- |
| placeholderColor         | [ResourceColor](ts-types.md#resourcecolor) | Placeholder text color.                      |
| placeholderFont          | [Font](ts-types.md#font) | Placeholder text style.                                   |
| textAlign                | [TextAlign](ts-appendix-enums.md#textalign) | Horizontal alignment of the text.<br>Default value: **TextAlign.Start**|
| caretColor               | [ResourceColor](ts-types.md#resourcecolor) | Color of the caret in the text box.                              |
| 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 ignored. The specified regular expression can match single characters, but not strings. Example: ^(?=.\*\d)(?=.\*[a-z])(?=.\*[A-Z]).{8,10}$. Strong passwords containing 8 to 10 characters cannot be filtered.<br>- **value**: regular expression to set.<br>- **error**: ignored content to return when regular expression matching fails.|
| copyOption<sup>9+</sup> | [CopyOptions](ts-appendix-enums.md#copyoptions9) | Whether copy and paste is allowed.|
Z
zengyawen 已提交
40 41 42

## Events

E
ester.zhou 已提交
43 44 45 46 47 48 49 50
In addition to the [universal events](ts-universal-events-click.md), the following events are supported.

| Name                                                        | Description                                                    |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| onChange(callback: (value: string) =&gt; void) | Triggered when the input changes.                                  |
| 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.|
| 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.|
| 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.|
Z
zengyawen 已提交
51

E
ester.zhou 已提交
52 53
## TextAreaController<sup>8+</sup>

E
esterzhou 已提交
54
Defines the controller for controlling the **\<TextArea>** component.
E
ester.zhou 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67

### Objects to Import

```
controller: TextAreaController = new TextAreaController()
```

### caretPosition<sup>8+</sup>

caretPosition(value: number): void

Sets the position of the caret.

E
ester.zhou 已提交
68 69 70 71 72
**Parameters**

| Name| Type| Mandatory| Description                              |
| ------ | -------- | ---- | -------------------------------------- |
| value  | number   | Yes  | Length from the start of the string to the position where the caret is located.|
E
ester.zhou 已提交
73

Z
zengyawen 已提交
74 75 76 77 78 79

## Example


### Multi-line Text Input

E
ester.zhou 已提交
80 81
```ts
// xxx.ets
Z
zengyawen 已提交
82 83
@Entry
@Component
E
ester.zhou 已提交
84
struct TextAreaExample1 {
E
ester.zhou 已提交
85
  controller: TextAreaController = new TextAreaController()
Z
zengyawen 已提交
86 87 88
  @State text: string = ''
  build() {
    Column() {
E
ester.zhou 已提交
89
      TextArea({ placeholder: 'input your word', controller: this.controller})
Z
zengyawen 已提交
90 91 92 93 94 95 96 97 98 99
        .placeholderColor("rgb(0,0,225)")
        .placeholderFont({ size: 30, weight: 100, family: 'cursive', style: FontStyle.Italic })
        .textAlign(TextAlign.Center)
        .caretColor(Color.Blue)
        .height(50)
        .fontSize(30)
        .fontWeight(FontWeight.Bold)
        .fontFamily("sans-serif")
        .fontStyle(FontStyle.Normal)
        .fontColor(Color.Red)
Z
zengyawen 已提交
100 101 102
        .inputFilter('^[\u4E00-\u9FA5A-Za-z0-9_]+$',(value: string) => {
          console.info("hyb"+value)
        })
Z
zengyawen 已提交
103 104
        .onChange((value: string) => {
          this.text = value
E
ester.zhou 已提交
105
          this.controller.caretPosition(-1)
Z
zengyawen 已提交
106 107 108 109 110 111 112
        })
      Text(this.text).width('90%')
    }
  }
}
```

Z
zengyawen 已提交
113 114 115
![en-us_image_0000001256858399](figures/en-us_image_0000001256858399.gif)


E
ester.zhou 已提交
116
### Setting the Caret
Z
zengyawen 已提交
117

E
ester.zhou 已提交
118 119
```ts
// xxx.ets
Z
zengyawen 已提交
120 121
@Entry
@Component
E
ester.zhou 已提交
122
struct TextAreaExample2 {
Z
zengyawen 已提交
123 124 125 126 127 128 129 130 131 132 133 134
    controller: TextAreaController = new TextAreaController()
    build() {
        Column() {
            TextArea({ placeholder: 'input your word',controller:this.controller })
            Button('caretPosition')
                .onClick(() => {
                    this.controller.caretPosition(4)
                })
          }
    }
}
```
Z
zengyawen 已提交
135

Z
zengyawen 已提交
136
![en-us_image_0000001212058476](figures/en-us_image_0000001212058476.png)