使用开始和结束索引,调用substring
from textpane.text
:
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, "<--");