• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# TextArea
2
3多行文本输入框组件,当输入的文本内容超过组件宽度时会自动换行显示。
4
5>  **说明:**
6>
7>  该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9
10## 子组件
11
1213
14
15## 接口
16
17TextArea(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextAreaController})
18
19**参数:**
20
21| 参数名                     | 参数类型                                     | 必填   | 参数描述           |
22| ----------------------- | ---------------------------------------- | ---- | -------------- |
23| placeholder      | [ResourceStr](ts-types.md#resourcestr)  | 否    | 设置无输入时的提示文本。输入内容后,提示文本不显示。<br/>仅设置placeholder属性时,手柄依然跟随拖动,手柄松开后光标停留在文字开头位置。     |
24| text             | [ResourceStr](ts-types.md#resourcestr)  | 否    | 设置输入框当前的文本内容。</br>当组件设置[stateStyles](ts-universal-attributes-polymorphic-style.md)等刷新属性时,建议通过onChange事件将状态变量与文本实时绑定,</br>避免组件刷新时TextArea中的文本内容异常。<br />从API version 10开始,该参数支持[$$](../../quick-start/arkts-two-way-sync.md)双向绑定变量。 |
25| controller<sup>8+</sup> | [TextAreaController](#textareacontroller8) | 否    | 设置TextArea控制器。 |
26
27
28## 属性
29
30除支持[通用属性](ts-universal-attributes-size.md)和[文本通用属性](ts-universal-attributes-text-style.md)的fontColor、fontSize、fontStyle、fontWeight、fontFamily外,还支持以下属性:
31
32| 名称                      | 参数类型                                                     | 描述                                                         |
33| ------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
34| placeholderColor          | [ResourceColor](ts-types.md#resourcecolor)                   | 设置placeholder文本颜色。<br/>默认值跟随主题。               |
35| placeholderFont           | [Font](ts-types.md#font)                                     | 设置placeholder文本样式,包括字体大小,字体粗细,字体族,字体风格。目前仅支持默认字体族。 |
36| textAlign                 | [TextAlign](ts-appendix-enums.md#textalign)                  | 设置文本在输入框中的水平对齐方式。<br/>默认值:TextAlign.Start<br/>**说明:**<br/>仅支持TextAlign.StartTextAlign.CenterTextAlign.End。<br/>可通过[align](ts-universal-attributes-location.md)属性控制文本段落在垂直方向上的位置,此组件中不可通过align属性控制文本段落在水平方向上的位置,即align属性中Alignment.TopStartAlignment.TopAlignment.TopEnd效果相同,控制内容在顶部,Alignment.StartAlignment.CenterAlignment.End效果相同,控制内容垂直居中,Alignment.BottomStartAlignment.BottomAlignment.BottomEnd效果相同,控制内容在底部。 |
37| caretColor                | [ResourceColor](ts-types.md#resourcecolor)                   | 设置输入框光标颜色。<br/>默认值:'#007DFF'。                 |
38| inputFilter<sup>8+</sup>  | {<br/>value:&nbsp;[ResourceStr](ts-types.md#resourcestr),<br/>error?:&nbsp;(value:&nbsp;string) => void<br/>} | 通过正则表达式设置输入过滤器。匹配表达式的输入允许显示,不匹配的输入将被过滤。仅支持单个字符匹配,不支持字符串匹配。<br/>-&nbsp;value:设置正则表达式。<br/>-&nbsp;error:正则匹配失败时,返回被过滤的内容。 |
39| copyOption<sup>9+</sup>   | [CopyOptions](ts-appendix-enums.md#copyoptions9)             | 设置输入的文本是否可复制。<br/>默认值:CopyOptions.LocalDevice,支持设备内复制。 <br/>设置CopyOptions.None时,当前TextArea中的文字无法被复制或剪切,仅支持粘贴。 |
40| maxLength<sup>10+</sup>   | number                                                       | 设置文本的最大输入字符数。<br/>默认不设置最大输入字符数限制。<br/>到达文本最大字符限制,将无法继续输入字符,同时边框变为红色。 |
41| showCounter<sup>10+</sup> | boolean                                                      | 设置文本最大输入字符数后,是否显示字数。<br/>默认值:false   |
42| style<sup>10+</sup>       | [TextContentStyle](ts-appendix-enums.md#textcontentstyle10)  | 设置文本框多态样式。<br/>默认值:TextContentStyle.DEFAULT    |
43| enableKeyboardOnFocus<sup>10+</sup> | boolean | TextArea获焦时,是否绑定输入法<br/>默认值:true。从API version 10开始,获焦默认绑定输入法。 |
44| selectionMenuHidden<sup>10+</sup> | boolean                                                      | 设置长按输入框或者右键输入框时,是否弹出文本选择菜单。<br />默认值:false |
45| barState<sup>10+</sup> | [BarState](ts-appendix-enums.md#BarState) | 设置内联输入风格编辑态时滚动条的显示模式。<br/>默认值:BarState.Auto |
46| maxLines<sup>10+</sup> | number | 设置内联输入风格编辑态时文本可显示的最大行数。<br/>默认值:3 |
47| customKeyboard<sup>10+</sup> | [CustomBuilder](ts-types.md#custombuilder8) | 设置自定义键盘。<br/>**说明:**<br/>当设置自定义键盘时,输入框激活后不会打开系统输入法,而是加载指定的自定义组件。<br/>自定义键盘的高度可以通过自定义组件根节点的height属性设置,宽度不可设置,使用系统默认值。<br/>自定义键盘采用覆盖原始界面的方式呈现,不会对应用原始界面产生压缩或者上提。<br/>自定义键盘无法获取焦点,但是会拦截手势事件。<br/>默认在输入控件失去焦点时,关闭自定义键盘,开发者也可以通过[TextAreaController](#textareacontroller8).[stopEditing](#stopediting10)方法控制键盘关闭。 |
48
49>  **说明:**
50>
51>  [通用属性padding](ts-universal-attributes-size.md)的默认值为:<br>{<br>&nbsp;top: 8 vp,<br>&nbsp;right: 16 vp,<br>&nbsp;bottom: 8 vp,<br>&nbsp;left: 16 vp<br> }
52
53## 事件
54
55除支持[通用事件](ts-universal-events-click.md)外,还支持以下事件:
56
57| 名称                                                         | 功能描述                                                     |
58| ------------------------------------------------------------ | ------------------------------------------------------------ |
59| onChange(callback:&nbsp;(value:&nbsp;string)&nbsp;=&gt;&nbsp;void) | 输入内容发生变化时,触发该回调。<br/>- value:当前输入的文本内容。 |
60| onEditChange(callback:&nbsp;(isEditing:&nbsp;boolean)&nbsp;=&gt;&nbsp;void)<sup>10+</sup> | 输入状态变化时,触发该回调。有光标时为编辑态,无光标时为非编辑态。isEditing为true表示正在输入。 |
61| onCopy<sup>8+</sup>(callback:(value:&nbsp;string)&nbsp;=&gt;&nbsp;void) | 长按输入框内部区域弹出剪贴板后,点击剪切板复制按钮,触发该回调。<br/>- value:复制的文本内容。 |
62| onCut<sup>8+</sup>(callback:(value:&nbsp;string)&nbsp;=&gt;&nbsp;void) | 长按输入框内部区域弹出剪贴板后,点击剪切板剪切按钮,触发该回调。<br/>- value:剪切的文本内容。 |
63| onPaste<sup>8+</sup>(callback:(value:&nbsp;string)&nbsp;=&gt;&nbsp;void) | 长按输入框内部区域弹出剪贴板后,点击剪切板粘贴按钮,触发该回调。<br/>- value:粘贴的文本内容。 |
64| onTextSelectionChange(callback: (selectionStart: number, selectionEnd: number) => void)<sup>10+</sup> | 文本选择的位置发生变化时,触发该回调。<br />selectionStart:文本选择区域起始位置,文本框中文字的起始位置为0。<br />selectionEnd:文本选择区域结束位置。 |
65| onContentScroll(callback: (totalOffsetX: number, totalOffsetY: number) => void)<sup>10+</sup> | 文本内容滚动时,触发该回调。<br />totalOffsetX:文本在内容区的横坐标偏移。<br />totalOffsetY:文本在内容区的纵坐标偏移。 |
66
67## TextAreaController<sup>8+</sup>
68
69TextArea组件的控制器,目前可通过它设置TextArea组件的光标位置。
70
71### 导入对象
72
73```
74controller: TextAreaController = new TextAreaController()
75```
76
77### caretPosition<sup>8+</sup>
78
79caretPosition(value: number): void
80
81设置输入光标的位置。
82
83**参数:**
84
85| 参数名 | 参数类型 | 必填 | 参数描述                               |
86| ------ | -------- | ---- | -------------------------------------- |
87| value  | number   | 是   | 从字符串开始到光标所在位置的字符长度。 |
88
89### setTextSelection<sup>10+</sup>
90
91setTextSelection(selectionStart: number, selectionEnd: number): void
92
93组件在获焦状态下,调用该接口设置文本选择区域并高亮显示,且只有在selectionStart小于selectionEnd时,文字才会被选取、高亮显示。
94
95**参数:**
96
97| 参数名         | 参数类型 | 必填 | 参数描述                                                     |
98| -------------- | -------- | ---- | ------------------------------------------------------------ |
99| selectionStart | number   | 是   | 文本选择区域起始位置,文本框中文字的起始位置为0。<br/>当selectionStart小于0时、按照0处理;当selectionStart大于文字最大长度时、按照文字最大长度处理。<br/> |
100| selectionEnd   | number   | 是   | 文本选择区域结束位置。<br/>当selectionEnd小于0时、按照0处理;当selectionEnd大于文字最大长度时、按照文字最大长度处理。<br/> |
101
102### stopEditing<sup>10+</sup>
103
104stopEditing(): void
105
106退出编辑态。
107
108### getTextContentRect<sup>10+</sup>
109
110getTextContentRect(): [RectResult](#rectresult)
111
112获取已编辑文本内容区域相对组件的位置和大小,返回值单位为像素。
113
114**返回值:**
115
116| 类型       | 说明       |
117| -------------------  | -------- |
118| [RectResult](#rectresult) | 已编辑文本内容的相对组件的位置和大小。 |
119
120> **说明:**
121>
122> - 初始不输入文本时,返回值中有相对组件的位置信息,大小为0。
123> - 返回值中的位置信息是第一个字符相对于可编辑组件的位置。
124> - 有输入时返回信息中的宽度是组件编辑的固定宽度。
125
126### RectResult<sup>10+</sup>
127
128位置和大小。
129
130| 参数      | 类型     | 描述 |
131| ------- | ------ | ----------------------- |
132| x     | number | 水平方向横坐标。|
133| y     | number | 竖直方向纵坐标。|
134| width | number | 内容宽度大小。|
135| height | number | 内容高度大小。|
136
137
138### getTextContentLineCount<sup>10+</sup>
139
140getTextContentLineCount(): number
141
142获取已编辑文本内容的行数。
143
144**返回值:**
145
146| 类型  | 说明       |
147| ----- | -------- |
148| number| 已编辑文本内容行数。 |
149
150## 示例
151
152### 示例1
153
154```ts
155// xxx.ets
156@Entry
157@Component
158struct TextAreaExample {
159  @State text: string = ''
160  controller: TextAreaController = new TextAreaController()
161
162  build() {
163    Column() {
164      TextArea({
165        text: this.text,
166        placeholder: 'The text area can hold an unlimited amount of text. input your word...',
167        controller: this.controller
168      })
169        .placeholderFont({ size: 16, weight: 400 })
170        .width(336)
171        .height(56)
172        .margin(20)
173        .fontSize(16)
174        .fontColor('#182431')
175        .backgroundColor('#FFFFFF')
176        .onChange((value: string) => {
177          this.text = value
178        })
179      Text(this.text)
180      Button('Set caretPosition 1')
181        .backgroundColor('#007DFF')
182        .margin(15)
183        .onClick(() => {
184          // 设置光标位置到第一个字符后
185          this.controller.caretPosition(1)
186        })
187    }.width('100%').height('100%').backgroundColor('#F1F3F5')
188  }
189}
190```
191
192![textArea](figures/textArea.gif)
193
194### 示例2
195
196```ts
197// xxx.ets
198@Entry
199@Component
200struct TextAreaExample {
201  @State text: string = 'test'
202  @State counterVisible: boolean = false
203  @State maxNumber: number = -1
204  controller: TextAreaController = new TextAreaController()
205
206  build() {
207    Column() {
208      TextArea({
209        text: this.text,
210        placeholder: 'The text area can hold an unlimited amount of text. input your word...',
211        controller: this.controller
212      })
213        .placeholderFont({ size: 16, weight: 400 })
214        .width(336)
215        .height(56)
216        .margin(20)
217        .fontSize(16)
218        .fontColor('#182431')
219        .maxLength(4)
220        .showCounter(true)
221        .backgroundColor('#FFFFFF')
222        .onChange((value: string) => {
223          this.text = value
224        })
225    }.width('100%').height('100%').backgroundColor('#F1F3F5')
226  }
227}
228```
229
230![maxLength](figures/maxLength.png)
231
232
233### 示例3
234
235```ts
236// xxx.ets
237@Entry
238@Component
239struct TextAreaExample {
240  controller: TextAreaController = new TextAreaController()
241  @State inputValue: string = ""
242
243  // 自定义键盘组件
244  @Builder CustomKeyboardBuilder() {
245    Column() {
246      Button('x').onClick(() => {
247        // 关闭自定义键盘
248        this.controller.stopEditing()
249      })
250      Grid() {
251        ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'], (item: number | string) => {
252          GridItem() {
253            Button(item + "")
254              .width(110).onClick(() => {
255              this.inputValue += item
256            })
257          }
258        })
259      }.maxCount(3).columnsGap(10).rowsGap(10).padding(5)
260    }.backgroundColor(Color.Gray)
261  }
262
263  build() {
264    Column() {
265      TextArea({ controller: this.controller, text: this.inputValue})
266        // 绑定自定义键盘
267        .customKeyboard(this.CustomKeyboardBuilder()).margin(10).border({ width: 1 })
268        .height(200)
269    }
270  }
271}
272```
273
274![customKeyboard](figures/textAreaCustomKeyboard.png)