使用开始和结束索引,从textpane.text调用substring:
var start:int = textpane.selectionBeginIndex;
var end:int = textpane.selectionEndIndex;
var text:String = textpane.text.substring(start, end);
TextField和TLFTextField实现replaceText()函数,可以插入文本。
在起始索引处替换:
textpane.replaceText(start, start, "-->");
在你的结束索引处替换:
textpane.replaceText(end, end, "<--");
要在开始和结束索引处都插入,请确保补偿插入文本的长度。
end += insertedText.length;
合起来就是:
// 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, "<--");