Class TextField

Used to enter text.

Class TextField

textfield:new () Constructor.
textfield:drawCursor (cursorX, cursorY, sx, sy) Draws the cursor.


Class TextField

Represents a text field.
textfield:new ()
Constructor.

  • text : a string. The initial text to fill the text field with.
  • maxLength : an integer (default 100). The maximum length allowed for the text inside this text field.
  • margin : an integer (default 10). The number of pixels to display the content of the textfield behind the left and right edges: useful to see whether there is text further on the left (right) when the cursor at the left (right) edge (see Fig.1 below).
  • width : an integer (default 100). The width of the text field. NOTE: margin is not included: it means that the full width would be width + 2 * margin (see Fig.1 below).

This block has 4 callbacks (see the on property in Block:new):

  • backspace : to erase one character under the cursor
  • left : to move the cursor one character left
  • right : to move the cursor one character right
  • textinput : to insert text (expects the text field in the passed event)

See focus.lua for the example.

Usage:

    local UI = ui.Layout {
      ui.TextField {
        width = 200,
      },
    
      ui.TextField {
        width = 100,
      },
    }
    
    function love.textinput(text)
      UI:push {
        id       = 'textinput',
        text     = text,
        filter   = function (this) return this.focus end
      }
    end
    -- without filter above both text fields will be receiving text
textfield:drawCursor (cursorX, cursorY, sx, sy)

Draws the cursor. Override this function if you want to draw the cursor in a custom way. This function will be called with 4 useful arguments (see below). Default cursor is just a one pixel wide vertical line.

Fig. 1
                                  sx
                    │◀────────────────────────────▶│
                    │                              │
                   margin       cursorX       margin  ┌──── not visible
                    │◀─▶│         │            │◀─▶│  │
   not visible ─┐   │   │         │            │   │  │
                ▼   ┌───┬─────────▼────────────┬───┐  ▼  ─▲─   ◀──── cursorY
             TEXT HE│RE │TEXT HERE TEXT HERE TE│XT │HERE  │  sy
             │      └───┼─────────▲────────────┼───┘     ─▼─
             │          │                      │
             │◀────────▶│◀────────────────────▶│
                shift            width

Parameters:

  • cursorX An integer. The x coordinate of the cursor position.
  • cursorY An integer. The y coordinate of the cursor position.
  • sx An integer. The width of the text bar.
  • sy An integer. The height of the text bar.
generated by LDoc 1.5.0 Last updated 2026-05-16 16:52:29