【问题标题】:actionscript TLF get selected textactionscript TLF 获取选定的文本
【发布时间】:2012-07-12 23:59:44
【问题描述】:

如何使用 actionscript 3 和 flash cs5 在 TLF 字段中捕获文本的潜文本?例如,我使用

获得了所选文本的偏移量
var zz:int = textpane.selectionBeginIndex;
var zzz:int = textpane.selectionEndIndex;

其中文本窗格是 TLF 框的一个实例。我得到了选择开始和结束位置的索引,但我不知道如何使用这些值来获取潜文本。

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

【问题讨论】:

    标签: actionscript-3 flash-cs5 tlf


    【解决方案1】:

    使用开始和结束索引,从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(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, "<--");
    

    【讨论】:

    • 谢谢!这帮了大忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多