2

How do I capture a subtext of the text in a TLF Field with actionscript 3 and flash cs5? For example, I have got the offsets of the selected text using

var zz:int = textpane.selectionBeginIndex;
var zzz:int = textpane.selectionEndIndex;

Where the textpane is an instance of the TLF box. I get the index of where the selection begins and ends but I cannot find out how to use those values to grab the subtext.

My ultimate goal is to add something before the text and something after the text dynamically rather than just replace it.

user1522256
  • 141
  • 1
  • 5

1 Answers1

0

Using the start and end index, call substring from textpane.text:

var start:int = textpane.selectionBeginIndex;
var end:int = textpane.selectionEndIndex;

var text:String = textpane.text.substring(start, end);

TextField and TLFTextField implement replaceText() function which can insert text.

To replace at your start index:

textpane.replaceText(start, start, "-->");

To replace at your end index:

textpane.replaceText(end, end, "<--");

To insert both at both start and end index, assure you compensate for the length of the inserted text.

end += insertedText.length;

All together, this becomes:

// find start and end positions
var start:int = textpane.selectionBeginIndex;
var end:int = textpane.selectionEndIndex;

// selected text
var text:String = textpane.text.substring(start, end);

// insert text at beginning of selection
var inseredtText:String = "-->";
textpane.replaceText(start, start, insertText);

// insert text at end of selection
end += insertedText.length;
textpane.replaceText(end, end, "<--");
Jason Sturges
  • 15,855
  • 14
  • 59
  • 80