Text area (lv_textarea)

Overview

The Text Area is a Page with a Label and a cursor on it. Texts or characters can be added to it. Long lines are wrapped and when the text becomes long enough the Text area can be scrolled.

Parts and Styles

The Text area has the same parts as Page. Expect LV_PAGE_PART_SCRL because it can't be referenced and it's always transparent. Refer the Page's documentation of details.

Besides the Page parts the virtual LV_TEXTAREA_PART_CURSOR part exists to draw the cursor. 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 opa to LV_TEXTAREA_PART_CURSOR's style. The create line cursor let the cursor transparent and set the border_side property.

Usage

Add text

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

  • lv_textarea_add_char(textarea, 'c')

  • lv_textarea_add_text(textarea, "insert this text")

To add wide characters like 'á', 'ß' or CJK characters use lv_textarea_add_text(ta, "á").

lv_textarea_set_text(ta, "New text") changes the whole text.

Placeholder

A placeholder text can be specified - which is displayed when the Text area is empty - with lv_textarea_set_placeholder_text(ta, "Placeholder text")

Delete character

To delete a character from the left of the current cursor position use lv_textarea_del_char(textarea). To delete from the right use lv_textarea_del_char_forward(textarea)

Move the cursor

The cursor position can be modified directly with lv_textarea_set_cursor_pos(textarea, 10). The 0 position means "before the first characters", LV_TA_CURSOR_LAST means "after the last character"

You can step the cursor with

  • lv_textarea_cursor_right(textarea)

  • lv_textarea_cursor_left(textarea)

  • lv_textarea_cursor_up(textarea)

  • lv_textarea_cursor_down(textarea)

If lv_textarea_set_cursor_click_pos(textarea, true) is called the cursor will jump to the position where the Text area was clicked.

Hide the cursor

The cursor can be hidden with lv_textarea_set_cursor_hidden(textarea, true).

One line mode

The Text area can be configures to be one lined with lv_textarea_set_one_line(ta, true). In this mode the height is set automatically to show only one line, line break character are ignored, and word wrap is disabled.

Password mode

The text area supports password mode which can be enabled with lv_textarea_set_pwd_mode(textarea, true).

If the (Bullet, U+2022) character exists in the font, the entered characters are converted to it after some time or when a new character is entered. If not exists, * will be used.

In password mode lv_textarea_get_text(textarea) gives the real text, not the bullet characters.

The visibility time can be adjusted with lv_textarea_set_pwd_show_time(textarea, time_ms).

Text align

The text can be aligned to the left, center or right with lv_textarea_set_text_align(textarea, LV_LABEL_ALIGN_LET/CENTER/RIGHT).

In one line mode, the text can be scrolled horizontally only if the text is left aligned.

Accepted characters

You can set a list of accepted characters with lv_textarae_set_accepted_chars(ta, "0123456789.+-"). Other characters will be ignored.

Max text length

The maximum number of characters can be limited with lv_textarea_set_max_length(textarea, max_char_num)

Very long texts

If there is a very long text in the Text area (e. g. > 20k characters) its scrolling and drawing might be slow. However, by enabling LV_LABEL_LONG_TXT_HINT   1 in lv_conf.h it can be hugely improved. It will save some info about the label to speed up its drawing. Using LV_LABEL_LONG_TXT_HINT the scrolling and drawing will as fast as with "normal" short texts.

Select text

A part of text can be selected if enabled with lv_textarea_set_text_sel(textarea, true). It works like when you select a text on your PC with your mouse.

Scrollbars

The scrollbars can shown according to different policies set by lv_textarea_set_scrollbar_mode(textarea, LV_SCRLBAR_MODE_...). Learn more at the Page object.

Scroll propagation

When the Text area is scrolled on an other scrollable object (like a Page) and the scrolling has reached the edge of the Text area, the scrolling can be propagated to the parent. In other words, when the Text area can be scrolled further, the parent will be scrolled instead.

It can be enabled with lv_ta_set_scroll_propagation(ta, true).

Learn more at the Page object.

Edge flash

When the Text area is scrolled to edge a circle like flash animation can be shown if it is enabled with lv_ta_set_edge_flash(ta, true)

Events

Besides the Generic events the following Special events are sent by the Slider:

  • LV_EVENT_INSERT Sent when before a character or text is inserted. The event data is the text planned to insert. lv_ta_set_insert_replace(ta, "New text") replaces the text to insert. The new text can't be in a local variable which is destroyed when the event callback exists. "" means do not insert anything.

  • LV_EVENT_VALUE_CHANGED When the content of the text area has been changed.

  • LV_EVENT_APPLY When LV_KEY_ENTER is sent to a text area which is in one line mode.

Keys

  • LV_KEY_UP/DOWN/LEFT/RIGHT Move the cursor

  • Any character Add the character to the current cursor position

Learn more about Keys.

Example

C

Simple Text area

code

#include "../../../lv_examples.h"
#include <stdio.h>
#if LV_USE_TEXTAREA

lv_obj_t * ta1;

static void event_handler(lv_obj_t * obj, lv_event_t event)
{
    if(event == LV_EVENT_VALUE_CHANGED) {
        printf("Value: %s\n", lv_textarea_get_text(obj));
    }
    else if(event == LV_EVENT_LONG_PRESSED_REPEAT) {
        /*For simple test: Long press the Text are to add the text below*/
        const char  * txt = "\n\nYou can scroll it if the text is long enough.\n";
        static uint16_t i = 0;
        if(txt[i] != '\0') {
            lv_textarea_add_char(ta1, txt[i]);
            i++;
        }
    }
}

void lv_ex_textarea_1(void)
{
    ta1 = lv_textarea_create(lv_scr_act(), NULL);
    lv_obj_set_size(ta1, 200, 100);
    lv_obj_align(ta1, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_textarea_set_text(ta1, "A text in a Text Area");    /*Set an initial text*/
    lv_obj_set_event_cb(ta1, event_handler);
}

#endif

Text area with password field

code

#include "../../../lv_examples.h"
#include <stdio.h>
#if LV_USE_TEXTAREA && LV_USE_KEYBOARD

static void ta_event_cb(lv_obj_t * ta, lv_event_t event);

static lv_obj_t * kb;

void lv_ex_textarea_2(void)
{
    /* Create the password box */
    lv_obj_t * pwd_ta = lv_textarea_create(lv_scr_act(), NULL);
    lv_textarea_set_text(pwd_ta, "");
    lv_textarea_set_pwd_mode(pwd_ta, true);
    lv_textarea_set_one_line(pwd_ta, true);
    lv_textarea_set_cursor_hidden(pwd_ta, true);
    lv_obj_set_width(pwd_ta, LV_HOR_RES / 2 - 20);
    lv_obj_set_pos(pwd_ta, 5, 20);
    lv_obj_set_event_cb(pwd_ta, ta_event_cb);

    /* Create a label and position it above the text box */
    lv_obj_t * pwd_label = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_text(pwd_label, "Password:");
    lv_obj_align(pwd_label, pwd_ta, LV_ALIGN_OUT_TOP_LEFT, 0, 0);

    /* Create the one-line mode text area */
    lv_obj_t * oneline_ta = lv_textarea_create(lv_scr_act(), pwd_ta);
    lv_textarea_set_pwd_mode(oneline_ta, false);
    lv_textarea_set_cursor_hidden(oneline_ta, true);
    lv_obj_align(oneline_ta, NULL, LV_ALIGN_IN_TOP_RIGHT, -5, 20);


    /* Create a label and position it above the text box */
    lv_obj_t * oneline_label = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_text(oneline_label, "Text:");
    lv_obj_align(oneline_label, oneline_ta, LV_ALIGN_OUT_TOP_LEFT, 0, 0);

    /* Create a keyboard */
    kb = lv_keyboard_create(lv_scr_act(), NULL);
    lv_obj_set_size(kb,  LV_HOR_RES, LV_VER_RES / 2);

    lv_keyboard_set_textarea(kb, pwd_ta); /* Focus it on one of the text areas to start */
    lv_keyboard_set_cursor_manage(kb, true); /* Automatically show/hide cursors on text areas */
}

static void ta_event_cb(lv_obj_t * ta, lv_event_t event)
{
    if(event == LV_EVENT_CLICKED) {
        /* Focus on the clicked text area */
        if(kb != NULL)
            lv_keyboard_set_textarea(kb, ta);
    }

    else if(event == LV_EVENT_INSERT) {
        const char * str = lv_event_get_data();
        if(str[0] == '\n') {
            printf("Ready\n");
        }
    }
}

#endif

Text auto-formatting

code

#include "../../../lv_examples.h"
#include <stdio.h>
#if LV_USE_TEXTAREA && LV_USE_KEYBOARD

static void ta_event_cb(lv_obj_t * ta, lv_event_t event);

static lv_obj_t * kb;

/**
 * Automatically format text like a clock. E.g. "12:34"
 * Add the ':' automatically.
 */
void lv_ex_textarea_3(void)
{
    /* Create the text area */
    lv_obj_t * ta = lv_textarea_create(lv_scr_act(), NULL);
    lv_obj_set_event_cb(ta, ta_event_cb);
    lv_textarea_set_accepted_chars(ta, "0123456789:");
    lv_textarea_set_max_length(ta, 5);
    lv_textarea_set_one_line(ta, true);
    lv_textarea_set_text(ta, "");

    /* Create a keyboard*/
    kb = lv_keyboard_create(lv_scr_act(), NULL);
    lv_obj_set_size(kb,  LV_HOR_RES, LV_VER_RES / 2);
    lv_keyboard_set_mode(kb, LV_KEYBOARD_MODE_NUM);
    lv_keyboard_set_textarea(kb, ta);
}

static void ta_event_cb(lv_obj_t * ta, lv_event_t event)
{
    if(event == LV_EVENT_VALUE_CHANGED) {
        const char * txt = lv_textarea_get_text(ta);
        if(txt[0] >= '0' && txt[0] <= '9' &&
            txt[1] >= '0' && txt[1] <= '9' &&
            txt[2] != ':')
        {
            lv_textarea_set_cursor_pos(ta, 2);
            lv_textarea_add_char(ta, ':');
        }
    }
}

#endif

MicroPython

No examples yet.

API

Typedefs

typedef uint8_t lv_textarea_style_t

Enums

enum [anonymous]

Possible text areas styles.

Values:

enumerator LV_TEXTAREA_PART_BG = LV_PAGE_PART_BG

Text area background style

enumerator LV_TEXTAREA_PART_SCROLLBAR = LV_PAGE_PART_SCROLLBAR

Scrollbar style

enumerator LV_TEXTAREA_PART_EDGE_FLASH = LV_PAGE_PART_EDGE_FLASH

Edge flash style

enumerator LV_TEXTAREA_PART_CURSOR = _LV_PAGE_PART_VIRTUAL_LAST

Cursor style

enumerator LV_TEXTAREA_PART_PLACEHOLDER

Placeholder style

enumerator _LV_TEXTAREA_PART_VIRTUAL_LAST
enumerator _LV_TEXTAREA_PART_REAL_LAST = _LV_PAGE_PART_REAL_LAST

Functions

LV_EXPORT_CONST_INT(LV_TEXTAREA_CURSOR_LAST)
lv_obj_t *lv_textarea_create(lv_obj_t *par, const lv_obj_t *copy)

Create a text area objects

Return

pointer to the created text area

Parameters
  • par: pointer to an object, it will be the parent of the new text area

  • copy: pointer to a text area object, if not NULL then the new object will be copied from it

void lv_textarea_add_char(lv_obj_t *ta, uint32_t c)

Insert a character to the current cursor position. To add a wide char, e.g. 'Á' use `_lv_txt_encoded_conv_wc('Á')`

Parameters
  • ta: pointer to a text area object

  • c: a character (e.g. 'a')

void lv_textarea_add_text(lv_obj_t *ta, const char *txt)

Insert a text to the current cursor position

Parameters
  • ta: pointer to a text area object

  • txt: a '\0' terminated string to insert

void lv_textarea_del_char(lv_obj_t *ta)

Delete a the left character from the current cursor position

Parameters
  • ta: pointer to a text area object

void lv_textarea_del_char_forward(lv_obj_t *ta)

Delete the right character from the current cursor position

Parameters
  • ta: pointer to a text area object

void lv_textarea_set_text(lv_obj_t *ta, const char *txt)

Set the text of a text area

Parameters
  • ta: pointer to a text area

  • txt: pointer to the text

void lv_textarea_set_placeholder_text(lv_obj_t *ta, const char *txt)

Set the placeholder text of a text area

Parameters
  • ta: pointer to a text area

  • txt: pointer to the text

void lv_textarea_set_cursor_pos(lv_obj_t *ta, int32_t pos)

Set the cursor position

Parameters
  • obj: pointer to a text area object

  • pos: the new cursor position in character index < 0 : index from the end of the text LV_TEXTAREA_CURSOR_LAST: go after the last character

void lv_textarea_set_cursor_hidden(lv_obj_t *ta, bool hide)

Hide/Unhide the cursor.

Parameters
  • ta: pointer to a text area object

  • hide: true: hide the cursor

void lv_textarea_set_cursor_click_pos(lv_obj_t *ta, bool en)

Enable/Disable the positioning of the the cursor by clicking the text on the text area.

Parameters
  • ta: pointer to a text area object

  • en: true: enable click positions; false: disable

void lv_textarea_set_pwd_mode(lv_obj_t *ta, bool en)

Enable/Disable password mode

Parameters
  • ta: pointer to a text area object

  • en: true: enable, false: disable

void lv_textarea_set_one_line(lv_obj_t *ta, bool en)

Configure the text area to one line or back to normal

Parameters
  • ta: pointer to a Text area object

  • en: true: one line, false: normal

void lv_textarea_set_text_align(lv_obj_t *ta, lv_label_align_t align)

Set the alignment of the text area. In one line mode the text can be scrolled only with LV_LABEL_ALIGN_LEFT. This function should be called if the size of text area changes.

Parameters
  • ta: pointer to a text are object

  • align: the desired alignment from lv_label_align_t. (LV_LABEL_ALIGN_LEFT/CENTER/RIGHT)

void lv_textarea_set_accepted_chars(lv_obj_t *ta, const char *list)

Set a list of characters. Only these characters will be accepted by the text area

Parameters
  • ta: pointer to Text Area

  • list: list of characters. Only the pointer is saved. E.g. "+-.,0123456789"

void lv_textarea_set_max_length(lv_obj_t *ta, uint32_t num)

Set max length of a Text Area.

Parameters
  • ta: pointer to Text Area

  • num: the maximal number of characters can be added (lv_textarea_set_text ignores it)

void lv_textarea_set_insert_replace(lv_obj_t *ta, const char *txt)

In LV_EVENT_INSERT the text which planned to be inserted can be replaced by an other text. It can be used to add automatic formatting to the text area.

Parameters
  • ta: pointer to a text area.

  • txt: pointer to a new string to insert. If "" no text will be added. The variable must be live after the event_cb exists. (Should be global or static)

void lv_textarea_set_scrollbar_mode(lv_obj_t *ta, lv_scrollbar_mode_t mode)

Set the scroll bar mode of a text area

Parameters
  • ta: pointer to a text area object

  • sb_mode: the new mode from 'lv_scrollbar_mode_t' enum

void lv_textarea_set_scroll_propagation(lv_obj_t *ta, bool en)

Enable the scroll propagation feature. If enabled then the Text area will move its parent if there is no more space to scroll.

Parameters
  • ta: pointer to a Text area

  • en: true or false to enable/disable scroll propagation

void lv_textarea_set_edge_flash(lv_obj_t *ta, bool en)

Enable the edge flash effect. (Show an arc when the an edge is reached)

Parameters
  • page: pointer to a Text Area

  • en: true or false to enable/disable end flash

void lv_textarea_set_text_sel(lv_obj_t *ta, bool en)

Enable/disable selection mode.

Parameters
  • ta: pointer to a text area object

  • en: true or false to enable/disable selection mode

void lv_textarea_set_pwd_show_time(lv_obj_t *ta, uint16_t time)

Set how long show the password before changing it to '*'

Parameters
  • ta: pointer to Text area

  • time: show time in milliseconds. 0: hide immediately.

Set cursor blink animation time

Parameters
  • ta: pointer to Text area

  • time: blink period. 0: disable blinking

const char *lv_textarea_get_text(const lv_obj_t *ta)

Get the text of a text area. In password mode it gives the real text (not '*'s).

Return

pointer to the text

Parameters
  • ta: pointer to a text area object

const char *lv_textarea_get_placeholder_text(lv_obj_t *ta)

Get the placeholder text of a text area

Return

pointer to the text

Parameters
  • ta: pointer to a text area object

lv_obj_t *lv_textarea_get_label(const lv_obj_t *ta)

Get the label of a text area

Return

pointer to the label object

Parameters
  • ta: pointer to a text area object

uint32_t lv_textarea_get_cursor_pos(const lv_obj_t *ta)

Get the current cursor position in character index

Return

the cursor position

Parameters
  • ta: pointer to a text area object

bool lv_textarea_get_cursor_hidden(const lv_obj_t *ta)

Get whether the cursor is hidden or not

Return

true: the cursor is hidden

Parameters
  • ta: pointer to a text area object

bool lv_textarea_get_cursor_click_pos(lv_obj_t *ta)

Get whether the cursor click positioning is enabled or not.

Return

true: enable click positions; false: disable

Parameters
  • ta: pointer to a text area object

bool lv_textarea_get_pwd_mode(const lv_obj_t *ta)

Get the password mode attribute

Return

true: password mode is enabled, false: disabled

Parameters
  • ta: pointer to a text area object

bool lv_textarea_get_one_line(const lv_obj_t *ta)

Get the one line configuration attribute

Return

true: one line configuration is enabled, false: disabled

Parameters
  • ta: pointer to a text area object

const char *lv_textarea_get_accepted_chars(lv_obj_t *ta)

Get a list of accepted characters.

Return

list of accented characters.

Parameters
  • ta: pointer to Text Area

uint32_t lv_textarea_get_max_length(lv_obj_t *ta)

Get max length of a Text Area.

Return

the maximal number of characters to be add

Parameters
  • ta: pointer to Text Area

lv_scrollbar_mode_t lv_textarea_get_scrollbar_mode(const lv_obj_t *ta)

Get the scroll bar mode of a text area

Return

scrollbar mode from 'lv_scrollbar_mode_t' enum

Parameters
  • ta: pointer to a text area object

bool lv_textarea_get_scroll_propagation(lv_obj_t *ta)

Get the scroll propagation property

Return

true or false

Parameters
  • ta: pointer to a Text area

bool lv_textarea_get_edge_flash(lv_obj_t *ta)

Get the scroll propagation property

Return

true or false

Parameters
  • ta: pointer to a Text area

bool lv_textarea_text_is_selected(const lv_obj_t *ta)

Find whether text is selected or not.

Return

whether text is selected or not

Parameters
  • ta: Text area object

bool lv_textarea_get_text_sel_en(lv_obj_t *ta)

Find whether selection mode is enabled.

Return

true: selection mode is enabled, false: disabled

Parameters
  • ta: pointer to a text area object

uint16_t lv_textarea_get_pwd_show_time(lv_obj_t *ta)

Set how long show the password before changing it to '*'

Return

show time in milliseconds. 0: hide immediately.

Parameters
  • ta: pointer to Text area

Set cursor blink animation time

Return

time blink period. 0: disable blinking

Parameters
  • ta: pointer to Text area

void lv_textarea_clear_selection(lv_obj_t *ta)

Clear the selection on the text area.

Parameters
  • ta: Text area object

void lv_textarea_cursor_right(lv_obj_t *ta)

Move the cursor one character right

Parameters
  • ta: pointer to a text area object

void lv_textarea_cursor_left(lv_obj_t *ta)

Move the cursor one character left

Parameters
  • ta: pointer to a text area object

void lv_textarea_cursor_down(lv_obj_t *ta)

Move the cursor one line down

Parameters
  • ta: pointer to a text area object

void lv_textarea_cursor_up(lv_obj_t *ta)

Move the cursor one line up

Parameters
  • ta: pointer to a text area object

struct lv_textarea_ext_t

Public Members

lv_page_ext_t page
lv_obj_t *label
char *placeholder_txt
lv_style_list_t style_placeholder
char *pwd_tmp
const char *accapted_chars
uint32_t max_length
uint16_t pwd_show_time
lv_style_list_t style
lv_coord_t valid_x
uint32_t pos
lv_area_t area
uint32_t txt_byte_pos
uint8_t state
uint8_t hidden
uint8_t click_pos
struct lv_textarea_ext_t::[anonymous] cursor
uint32_t sel_start
uint32_t sel_end
uint8_t text_sel_in_prog
uint8_t text_sel_en
uint8_t pwd_mode
uint8_t one_line