如何使用actionscript 3和flash cs5捕获TLF字段中文本的子文本?例如,我使用
获得了所选文本的偏移量var zz:int = textpane.selectionBeginIndex;
var zzz:int = textpane.selectionEndIndex;
其中textpane是TLF框的实例。我得到了选择开始和结束的索引但我无法找到如何使用这些值来获取子文本。
我的最终目标是在文本之前添加一些内容,在文本之后添加一些内容,而不是仅仅替换它。
答案 0 :(得分:0)
使用开始和结束索引,从substring
:
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, "<--");