object:setSelection()

Type Function
Object TextField
Library native.*
Return value none
Revision Release 2022.3683
Keywords setSelection
See also native.newTextField()

Overview

Sets the cursor position if the start and end positions are the same. Alternatively, sets a range of selected text if the start and end positions are different.

Syntax

object:setSelection( startPosition, endPosition )
startPosition (required)

Number. The cursor position or the beginning of the selection area. To select the first character, this value should be 0.

endPosition (required)

Number. The end of the selection area (if greater than startPosition). The last character will be used if this number is greater than the length of the current string in the text field.

Gotchas

This API does not work in the Corona Simulator for macOS.

Example

local textField = native.newTextField( 160, 240, 280, 40 )

textField.text = "asdf"
native.setKeyboardFocus( textField )

textField:setSelection( 1, 3 )  -- The characters "sd" will be selected
textField:setSelection( 1, 1 )  -- Cursor will be placed between "a" and "s"