# Text Area (lv_textarea) (/widgets/textarea)



Overview [#overview]

A Text Area is a [base widget](/widgets/base_widget) holding a
[Label](/widgets/label) and a cursor. You can add or remove text and
characters. Long lines wrap, and when the text gets too long the area
becomes scrollable.

One-line mode and password mode are also supported.

Adding text [#adding-text]

<LvglExample name="lv_example_textarea_buttonmatrix" path="widgets/textarea/lv_example_textarea_buttonmatrix" />

You can insert text or characters to the current cursor's position with:

* <ApiLink name="lv_textarea_add_char" display="lv_textarea_add_char(textarea, 'c')" />
* <ApiLink name="lv_textarea_add_text" display="lv_textarea_add_text(textarea, &#x22;insert this text&#x22;)" />

To add wide characters like `'á'`, `'ß'` or CJK characters, use
<ApiLink name="lv_textarea_add_text" display="lv_textarea_add_text(textarea, &#x22;á&#x22;)" />.

<ApiLink name="lv_textarea_set_text" display="lv_textarea_set_text(textarea, &#x22;New text&#x22;)" /> replaces all existing text
with "New text".

Placeholder text [#placeholder-text]

<LvglExample name="lv_example_textarea_placeholder" path="widgets/textarea/lv_example_textarea_placeholder" />

Placeholder text is text that is displayed when the Text Area is empty.  This can be
a handy way to provide the end user with a hint about what to type there.

Specify placeholder text using
<ApiLink name="lv_textarea_set_placeholder_text" display="lv_textarea_set_placeholder_text(textarea, &#x22;Placeholder text&#x22;)" />.

Delete character [#delete-character]

To delete the character to the left of the current cursor position, use
<ApiLink name="lv_textarea_delete_char" display="lv_textarea_delete_char(textarea)" />.

To delete to the right, use <ApiLink name="lv_textarea_delete_char_forward" display="lv_textarea_delete_char_forward(textarea)" />

Moving the cursor [#moving-the-cursor]

The cursor position can be modified programmatically using
<ApiLink name="lv_textarea_set_cursor_pos" display="lv_textarea_set_cursor_pos(textarea, cursor_pos)" /> where `cursor_pos` is
the zero-based index of the character the cursor should be placed in front of.
<ApiLink name="LV_TEXTAREA_CURSOR_LAST" /> can be passed to mean "after the
last character"

You can move the cursor one character-position (or line) at a time with

* <ApiLink name="lv_textarea_cursor_right" display="lv_textarea_cursor_right(textarea)" />
* <ApiLink name="lv_textarea_cursor_left" display="lv_textarea_cursor_left(textarea)" />
* <ApiLink name="lv_textarea_cursor_up" display="lv_textarea_cursor_up(textarea)" />
* <ApiLink name="lv_textarea_cursor_down" display="lv_textarea_cursor_down(textarea)" />

If <ApiLink name="lv_textarea_set_cursor_click_pos" display="lv_textarea_set_cursor_click_pos(textarea, true)" /> is applied, the
cursor will jump to the position where the Text Area was clicked.

Hiding the cursor [#hiding-the-cursor]

The cursor is normally always visible. It can be a good idea to style it
to be visible only in <ApiLink name="LV_STATE_FOCUSED" /> state. See [Styles](/common-widget-features/styles)
for more information about how to do this.

One-line mode [#one-line-mode]

The Text Area can be configured to keep all text on a single line with
<ApiLink name="lv_textarea_set_one_line" display="lv_textarea_set_one_line(textarea, true)" />. In this mode:

* the height is set automatically to show only one line,
* line break characters are ignored, and
* word wrap is disabled.

Password mode [#password-mode]

<LvglExample name="lv_example_textarea_password" path="widgets/textarea/lv_example_textarea_password" />

The Text Area supports password mode which can be enabled with
<ApiLink name="lv_textarea_set_password_mode" display="lv_textarea_set_password_mode(textarea, true)" />.

By default, if the [•` (`Bullet,
U+2022](https://www.fileformat.info/info/unicode/char/2022/index.htm))
character exists in the font, the entered characters are converted to it after
a configurable delay after each new character is entered. If `•` does not
exist in the font, `*` will be used. You can override the default
"masking" character with <ApiLink name="lv_textarea_set_password_bullet" display="lv_textarea_set_password_bullet(textarea, str)" />
where `str` is a NUL-terminated C string.  Example:

```c title=" " lineNumbers=1
lv_textarea_set_password_bullet(textarea, "x");
```

In password mode <ApiLink name="lv_textarea_get_text" display="lv_textarea_get_text(textarea)" /> returns the actual
text entered, not the bullet characters.

The visibility time can be adjusted with <ApiLink name="LV_TEXTAREA_DEF_PWD_SHOW_TIME" /> in `lv_conf.h`.

Accepted characters [#accepted-characters]

<LvglExample name="lv_example_textarea_format" path="widgets/textarea/lv_example_textarea_format" />

You can set a list of accepted characters with
<ApiLink name="lv_textarea_set_accepted_chars" display="lv_textarea_set_accepted_chars(textarea, list)" /> or
<ApiLink name="lv_textarea_set_accepted_chars_static" display="lv_textarea_set_accepted_chars_static(textarea, list)" /> where `list` is a
pointer to a NUL-terminated string, or NULL to accept all characters.  Characters
entered not in this list will be ignored.

```c title=" " lineNumbers=1
lv_textarea_set_accepted_chars(textarea, "0123456789.+-");
```

Max text length [#max-text-length]

The maximum number of characters can be limited using
<ApiLink name="lv_textarea_set_max_length" display="lv_textarea_set_max_length(textarea, max_char_num)" />.

Very long text [#very-long-text]

For text exceeding \~20k characters, enable <ApiLink name="LV_LABEL_LONG_TXT_HINT" /> in
`lv_conf.h` to improve scrolling and drawing performance by caching vertical
position data. It is set to `1` by default.

Selecting text [#selecting-text]

Enable text selection with
<ApiLink name="lv_textarea_set_text_selection" display="lv_textarea_set_text_selection(textarea, true)" />.
Requires <ApiLink name="LV_LABEL_TEXT_SELECTION" /> set to a non-zero value in `lv_conf.h`.

To manipulate the selection programmatically (`ta_label = lv_textarea_get_label(textarea)`):

* <ApiLink name="lv_textarea_get_text_selection" display="lv_textarea_get_text_selection(textarea)" /> — whether selection is enabled.
* <ApiLink name="lv_textarea_text_is_selected" display="lv_textarea_text_is_selected(textarea)" /> — whether text is currently selected.
* <ApiLink name="lv_textarea_clear_selection" display="lv_textarea_clear_selection(textarea)" /> — clears the selection.
* <ApiLink name="lv_label_set_text_selection_start" display="lv_label_set_text_selection_start(ta_label, index)" /> — start index; pass <ApiLink name="LV_DRAW_LABEL_NO_TXT_SEL" /> for none.
* <ApiLink name="lv_label_set_text_selection_end" display="lv_label_set_text_selection_end(ta_label, index)" /> — end index (exclusive); pass <ApiLink name="LV_DRAW_LABEL_NO_TXT_SEL" /> for none.
* <ApiLink name="lv_label_get_text_selection_start" display="lv_label_get_text_selection_start(ta_label)" /> — gets start index.
* <ApiLink name="lv_label_get_text_selection_end" display="lv_label_get_text_selection_end(ta_label)" /> — gets end index.

Styling [#styling]

<LvglExample name="lv_example_textarea_cursor" path="widgets/textarea/lv_example_textarea_cursor" />

The Text Area exposes these parts, each styled independently with a named
style and a `selector`:

* <ApiLink name="LV_PART_MAIN" /> The background of the Text Area; uses the
  [typical background style properties](/common-widget-features/styles/overview) and the text related
  style properties including `text_align` to align the text to the left,
  right or center.
* <ApiLink name="LV_PART_SCROLLBAR" /> The scrollbar that is shown when the text is
  longer than its height.
* <ApiLink name="LV_PART_SELECTED" /> Determines the style of the [selected
  text](/widgets/label). Only `text_color` and
  `bg_color` style properties can be used. `bg_color` should be set
  directly on the label of the Text Area.
* <ApiLink name="LV_PART_CURSOR" /> Marks the position where the characters are
  inserted. The cursor's area is always the bounding box of the current
  character. A block cursor can be created by adding a background color
  and background opacity to <ApiLink name="LV_PART_CURSOR" />'s style. To create
  a "bar" cursor leave the cursor transparent and set a left border. The
  `anim_time` style property sets the cursor's blink time.
* <ApiLink name="LV_PART_TEXTAREA_PLACEHOLDER" /> Unique to Text Area; allows styling
  the [placeholder text](/widgets/textarea).

Events [#events]

* <ApiLink name="LV_EVENT_INSERT" /> Sent just before a character or text is
  inserted. The event parameter is the text about to be inserted. From the
  handler you can call
  <ApiLink name="lv_textarea_set_insert_replace" display="lv_textarea_set_insert_replace(textarea, &#x22;New text&#x22;)" />
  to replace it, or pass `""` to skip the insertion. The buffer must stay
  valid until the current `lv_timer_handler()` call returns — don't pass
  a stack-allocated string.
* <ApiLink name="LV_EVENT_VALUE_CHANGED" /> Sent when the content of the Text Area
  has changed.
* <ApiLink name="LV_EVENT_READY" /> Sent when <ApiLink name="LV_KEY_ENTER" /> is
  pressed (or sent) to a one-line Text Area.

Learn more about [Events](/common-widget-features/events) emitted by all Widgets.

Keys [#keys]

* `LV_KEY_UP/DOWN/LEFT/RIGHT` Move the cursor
* `Any character` Add the character to the current cursor position

Learn more about [Keys](/main-modules/indev/keypad).
