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


4 5
> **NOTE**<br>
> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

Z
zengyawen 已提交
7

E
ester.zhou 已提交
8
The **\<TextArea>** component provides multi-line text input.
Z
zengyawen 已提交
9 10 11


## Required Permissions
Z
zengyawen 已提交
12 13 14

None

Z
zengyawen 已提交
15

E
esterzhou 已提交
16
## Child Components
Z
zengyawen 已提交
17

E
esterzhou 已提交
18
None
Z
zengyawen 已提交
19

Z
zengyawen 已提交
20 21 22 23 24 25

## APIs

TextArea(value?:{placeholder?: string controller?: TextAreaController})

- Parameters
H
HelloCrease 已提交
26 27 28
    | Name                    | Type                                     | Mandatory | Default Value | Description                            |
    | ----------------------- | ---------------------------------------- | --------- | ------------- | -------------------------------------- |
    | placeholder             | string                                   | No        | -             | Text displayed when there is no input. |
29
    | controller<sup>8+</sup> | [TextAreaController](#textareacontroller8) | No      | -             | Text area controller.                  |
Z
zengyawen 已提交
30 31 32 33


## Attributes

E
ester.zhou 已提交
34
In addition to universal attributes, the following attributes are supported.
Z
zengyawen 已提交
35

H
HelloCrease 已提交
36 37 38 39
| Name                     | Type                                     | Default Value | Description                              |
| ------------------------ | ---------------------------------------- | ------------- | ---------------------------------------- |
| placeholderColor         | Color                                    | -             | Placeholder text color.                  |
| placeholderFont          | {<br/>size?: number,<br/>weight?:number \| [FontWeight](ts-universal-attributes-text-style.md),<br/>family?: string,<br/>style?: [FontStyle](ts-universal-attributes-text-style.md)<br/>} | -             | Placeholder text style.<br/>- **size**: font size. If the value is of the number type, the unit fp is used.<br/>- **weight**: font weight. For the number type, the value ranges from 100 to 900, at an interval of 100. The default value is **400**. A larger value indicates a larger font weight.<br/>- **family**: font family. Use commas (,) to separate multiple fonts, for example, **'Arial, sans-serif'**. The priority of the fonts is the sequence in which they are placed.<br/>- **style**: font style. |
E
ester.zhou 已提交
40 41 42 43
| textAlign                | TextAlign                                | Start         | Text horizontal alignment mode. |
| caretColor               | Color                                    | -             | Color of the caret in the text box. |
| inputFilter<sup>8+</sup> | {<br/>value: [ResourceStr](../../ui/ts-types.md)<sup>8+</sup>,<br/>error?: (value:&nbsp;string)<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**: indicates the regular expression to set.<br/>- **error**: returns the ignored content when regular expression matching fails. |
| copyOption<sup>9+</sup> | boolean\|[CopyOption](ts-basic-components-text.md) | true | Whether copy and paste is allowed. |
Z
zengyawen 已提交
44 45

- TextAlign enums
H
HelloCrease 已提交
46 47 48 49 50
    | Name   | Description                     |
    | ------ | ------------------------------- |
    | Start  | Aligns the header horizontally. |
    | Center | Horizontal center alignment.    |
    | End    | Align the tail horizontally.    |
Z
zengyawen 已提交
51 52 53 54


## Events

H
HelloCrease 已提交
55 56 57
| Name                                     | Description                              |
| ---------------------------------------- | ---------------------------------------- |
| onChange(callback: (value: string) =&gt; void) | Triggered when the input changes.        |
E
esterzhou 已提交
58 59 60
| 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 已提交
61

E
ester.zhou 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
## TextAreaController<sup>8+</sup>

Defines the controller for controlling the **&lt;TextArea&gt;** component.

### Objects to Import

```
controller: TextAreaController = new TextAreaController()

```

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

caretPosition(value: number): void

Sets the position of the caret.

- Parameters
E
ester.zhou 已提交
80 81 82
  | Name  | Type   | Mandatory | Default Value | Description                                                  |
  | ----- | ------ | --------- | ------------- | ------------------------------------------------------------ |
  | value | number | Yes       | -             | Length from the start of the text string to the position where the caret is located. |
E
ester.zhou 已提交
83

Z
zengyawen 已提交
84 85 86 87 88 89

## Example


### Multi-line Text Input

Z
zengyawen 已提交
90

E
ester.zhou 已提交
91 92
```ts
// xxx.ets
Z
zengyawen 已提交
93 94
@Entry
@Component
E
ester.zhou 已提交
95
struct TextAreaExample1 {
Z
zengyawen 已提交
96 97 98
  @State text: string = ''
  build() {
    Column() {
Z
zengyawen 已提交
99
      TextArea({ placeholder: 'input your word'})
Z
zengyawen 已提交
100 101 102 103 104 105 106 107 108 109
        .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 已提交
110 111 112
        .inputFilter('^[\u4E00-\u9FA5A-Za-z0-9_]+$',(value: string) => {
          console.info("hyb"+value)
        })
Z
zengyawen 已提交
113 114 115 116 117 118 119 120 121
        .onChange((value: string) => {
          this.text = value
        })
      Text(this.text).width('90%')
    }
  }
}
```

Z
zengyawen 已提交
122 123 124
![en-us_image_0000001256858399](figures/en-us_image_0000001256858399.gif)


E
ester.zhou 已提交
125
### Setting the Caret
Z
zengyawen 已提交
126

E
ester.zhou 已提交
127 128
```ts
// xxx.ets
Z
zengyawen 已提交
129 130
@Entry
@Component
E
ester.zhou 已提交
131
struct TextAreaExample2 {
Z
zengyawen 已提交
132 133 134 135 136 137 138 139 140 141 142 143
    controller: TextAreaController = new TextAreaController()
    build() {
        Column() {
            TextArea({ placeholder: 'input your word',controller:this.controller })
            Button('caretPosition')
                .onClick(() => {
                    this.controller.caretPosition(4)
                })
          }
    }
}
```
Z
zengyawen 已提交
144

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