Recipe 9.21. Selecting Text with ActionScript


Problem

You want to highlight a portion of the text within a text field.

Solution

Use the TextField.setSelection( ) method.

Discussion

The TextField.setSelection( ) method highlights a portion of the text in the text field. The setSelection( ) method takes two parameters:


startIndex

The beginning, zero-relative index of the text to highlight.


endIndex

The index of the character after the text to highlight.

For the selection to work, the text field must have focus, which you can set by using Stage.focus, as discussed in Recipe 9.20:

stage.focus = field;                  // Set the focus to the text field field.text = "this is example text";  // Set the text value field.setSelection(0, 4);             // Highlight the word "this"

Use the read-only selectionBeginIndex and selectionEndIndex properties to retrieve the indices of the selected character range.

See Also

Recipes 9.20 and 9.23




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net