object:setSelection()

Type Function
Object TextBox
Library native.*
Return value none
Revision Release 2024.3703
Keywords setSelection
See also native.newTextBox()

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 box.

Example

local textBox = native.newTextBox( 160, 240, 280, 140 )

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

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