2012-07-12 67 views
2

如何捕获带有actionscript 3和flash cs5的TLF字段中文本的子文本?举例来说,我一直在使用actionscript TLF获取所选文本

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

凡textpane是TLF盒的一个实例得到了所选文本的偏移量。我得到了选择开始和结束的索引,但我无法找到如何使用这些值来获取潜台词。

我的最终目标是动态地在文本之前和文本之后添加一些内容,而不是仅仅替换它。

回答

0

使用的开始和结束索引,从textpane.text调用substring

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

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

TextFieldTLFTextField实现replaceText()功能,可以插入文本。

会在开始指数更换:

要在您的最终指数更换:

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, "<--"); 
+0

谢谢!这有助于堆积如山。 – user1522256 2012-07-13 01:16:18