Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 如何在文本流中插入元素?_Actionscript 3_Apache Flex - Fatal编程技术网

Actionscript 3 如何在文本流中插入元素?

Actionscript 3 如何在文本流中插入元素?,actionscript-3,apache-flex,Actionscript 3,Apache Flex,我希望能够在文本区域中选择一些文本,然后用元素替换所选文本。我无法使用TextLayoutFormat,因为它没有“id”属性 如何在文本区域的特定位置插入跨距?有几种方法。根据起始文本的结构,您可以使用一种简单的方法来替换文本,或者您可能需要一种更通用的方法来处理许多场景 两个想法,一个简单,一个接近通用解决方案。我很想看到其他的方法,我一直在学习关于TLF的新东西:) 将TLF文本导出为字符串,进行替换,将字符串重新导入新的TLF文本流。 从Spark的TextFlow属性获取TextFlo

我希望能够在文本区域中选择一些文本,然后用元素替换所选文本。我无法使用TextLayoutFormat,因为它没有“id”属性


如何在文本区域的特定位置插入跨距?

有几种方法。根据起始文本的结构,您可以使用一种简单的方法来替换文本,或者您可能需要一种更通用的方法来处理许多场景

两个想法,一个简单,一个接近通用解决方案。我很想看到其他的方法,我一直在学习关于TLF的新东西:)

将TLF文本导出为字符串,进行替换,将字符串重新导入新的TLF文本流。

  • 从Spark的
    TextFlow
    属性获取TextFlow text区域/text输入
  • 使用TextFlow.getText(0,-1,“
    ”)获取字符串表示形式
  • 替换是否重新导入流:查看TextFlowUtil.importFromString
  • 将上述步骤中的文本流应用于TextInput或TextArea的
    textFlow
    属性
  • 这种方法可能不是最有效的。注意,您还可能会丢失应用于文本的任何样式。看看TextConverter类,您也可以使用它将文本导入到TLF w/a文本格式。特别是TextConvert.importToFlow()

    使用TextFlow API获取包含文本的FlowElement。

    删除包含要替换的原始文本的元素,追加替换文本,追加结束文本。比上述方法更有效。注意,如果你知道你的文本的确切结构,你可以使这更简单

    [编辑]

    以下是一个同时具有以下两种功能的示例:

    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   width="800" height="600" xmlns:local="*">
    
        <fx:Script>
            <![CDATA[
                import flashx.textLayout.edit.ISelectionManager;
                import flashx.textLayout.elements.FlowElement;
                import flashx.textLayout.elements.TextFlow;
    
                import spark.utils.TextFlowUtil;
    
                private var flow:TextFlow;
                private var mgr:ISelectionManager;
                private var startIndex:int;
                private var endIndex:int;
    
                private function init():void
                {
                    flow = texty.textFlow;
                    mgr = flow.interactionManager;
                    if (mgr.hasSelection())
                    {
                        // determine start/end indexes of the selection
                        if (mgr.anchorPosition < mgr.activePosition)
                        {
                            startIndex = mgr.anchorPosition;
                            endIndex = mgr.activePosition;
                        }
                        else
                        {
                            startIndex = mgr.activePosition;
                            endIndex = mgr.anchorPosition;
                        }
                    }
                    else
                    {
                        startIndex = 0;
                        endIndex = 0;
                    }
                }
    
                private function replaceByImportText():void
                {
                    init();
                    if (mgr.hasSelection())
                    {
                        var copy:String = flow.getText(0,-1,"<br/>");
                        // replace the text
                        copy = copy.substring(0,startIndex) + "*** You just replaced me ***" + copy.substring(endIndex);
                        // reimport - expensive (makes a new TextFlow) and probably looses some fidelity (styles, etc)
                        texty.textFlow = TextFlowUtil.importFromString(copy);
                    }
                }
    
                private function replaceBySplittingFlowElements():void
                {
                    init();
                    if (mgr.hasSelection())
                    {
                        // each call to splitAtPosition leaves the text before startIndex in the original FlowElement
                        // and returns a new FlowElement (TextFlow's in this case) w/the remaining text
                        var textToBeReplaced:FlowElement = flow.splitAtPosition(startIndex);
                        // chops out whatever was between start/end indexes
                        var remainingTextToAddBack:TextFlow = textToBeReplaced.splitAtPosition(endIndex - startIndex) as TextFlow;
                        // insert replacment text
                        var span:SpanElement = new SpanElement();
                        span.text = "Hi, I'm a replacement String.";
                        // assumes last element is a paragraph but it could be a div :)
                        var lastParagraph:ParagraphElement = flow.getChildAt(flow.numChildren -1) as ParagraphElement;
                        if (lastParagraph)
                        {
                            lastParagraph.addChild(span);
                            // merge the last paragraph w/the 1st paragraph of remaining text
                            var firstP:ParagraphElement = remainingTextToAddBack.getChildAt(0) as ParagraphElement;
                            if (firstP)
                            {
                                lastParagraph.replaceChildren( lastParagraph.numChildren, lastParagraph.numChildren, getParagraphChildren(firstP) );
                            }
    
                        }
    
                    }
                }
    
                private function getParagraphChildren(p:ParagraphElement):Array
                {
                    var kids:Array =[];
                    var numKids:int = p.numChildren;
                    for (var i:int = 0; i<numKids; i++)
                    {
                        kids.push( p.getChildAt(i) );
                    }
                    return kids;
                }
    
            ]]>
        </fx:Script>
    
        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
    
        <s:HGroup>
            <s:Button label="Replace by importing text" click="replaceByImportText()" />
            <s:Button label="Replace by using FlowElements" click="replaceBySplittingFlowElements()"/>
        </s:HGroup>
    
    
        <s:TextArea id="texty" top="40" left="40" right="40" bottom="40">
            <s:p>
                <s:span>
                    <s:text>
                        Bacon ipsum dolor sit amet turducken pancetta short ribs tail anim pig.
                        In pastrami in, ball tip flank shankle beef ribs spare ribs deserunt pancetta esse cupidatat aliquip venison pork chop.
                        Pork loin commodo corned beef ullamco culpa dolore occaecat, capicola adipisicing ribeye bresaola sunt est. 
                        Commodo labore culpa ut, sausage ad meatloaf adipisicing.
                    </s:text>
                </s:span>
                <s:br/>
            </s:p>
    
            <s:p>
                <s:span>
                    <s:text>
                        Quis qui aliqua enim, rump jerky aute bresaola aliquip pig speck short loin.
                        Non ea eiusmod shoulder, consequat enim ribeye sed. Meatloaf tenderloin pork loin reprehenderit.
                        Enim rump eiusmod, tri-tip capicola in do frankfurter dolore.
                        Culpa elit meatball pariatur turducken, leberkas excepteur irure in pork belly shank consequat.
                        Sint biltong t-bone veniam shankle. Consectetur irure et minim.
                    </s:text>
                </s:span>
                <s:br/>
            </s:p>
    
            <s:p>
                <s:span>
                    <s:text>
                        Excepteur laborum non corned beef, est dolore pastrami jowl id.
                        Leberkas short ribs ham, tempor id sed esse. Officia eiusmod pork frankfurter leberkas cow, nisi qui filet mignon mollit swine bacon veniam.
                        Nostrud deserunt nulla ground round, shankle sausage aliqua ut frankfurter culpa.
                        Veniam hamburger spare ribs, ullamco dolor labore salami capicola short loin swine.
                    </s:text>
                </s:span>
            </s:p>
    
        </s:TextArea>
    </s:Application>
    
    
    
    有几种方法可以做到这一点。根据起始文本的结构,您可以使用一种简单的方法来替换文本,或者您可能需要一种更通用的方法来处理许多场景

    两个想法,一个简单,一个接近通用解决方案。我很想看到其他的方法,我一直在学习关于TLF的新东西:)

    将TLF文本导出为字符串,进行替换,将字符串重新导入新的TLF文本流。

  • 从Spark的
    TextFlow
    属性获取TextFlow text区域/text输入
  • 使用TextFlow.getText(0,-1,“
    ”)获取字符串表示形式
  • 替换是否重新导入流:查看TextFlowUtil.importFromString
  • 将上述步骤中的文本流应用于TextInput或TextArea的
    textFlow
    属性
  • 这种方法可能不是最有效的。注意,您还可能会丢失应用于文本的任何样式。看看TextConverter类,您也可以使用它将文本导入到TLF w/a文本格式。特别是TextConvert.importToFlow()

    使用TextFlow API获取包含文本的FlowElement。

    删除包含要替换的原始文本的元素,追加替换文本,追加结束文本。比上述方法更有效。注意,如果你知道你的文本的确切结构,你可以使这更简单

    [编辑]

    以下是一个同时具有以下两种功能的示例:

    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   width="800" height="600" xmlns:local="*">
    
        <fx:Script>
            <![CDATA[
                import flashx.textLayout.edit.ISelectionManager;
                import flashx.textLayout.elements.FlowElement;
                import flashx.textLayout.elements.TextFlow;
    
                import spark.utils.TextFlowUtil;
    
                private var flow:TextFlow;
                private var mgr:ISelectionManager;
                private var startIndex:int;
                private var endIndex:int;
    
                private function init():void
                {
                    flow = texty.textFlow;
                    mgr = flow.interactionManager;
                    if (mgr.hasSelection())
                    {
                        // determine start/end indexes of the selection
                        if (mgr.anchorPosition < mgr.activePosition)
                        {
                            startIndex = mgr.anchorPosition;
                            endIndex = mgr.activePosition;
                        }
                        else
                        {
                            startIndex = mgr.activePosition;
                            endIndex = mgr.anchorPosition;
                        }
                    }
                    else
                    {
                        startIndex = 0;
                        endIndex = 0;
                    }
                }
    
                private function replaceByImportText():void
                {
                    init();
                    if (mgr.hasSelection())
                    {
                        var copy:String = flow.getText(0,-1,"<br/>");
                        // replace the text
                        copy = copy.substring(0,startIndex) + "*** You just replaced me ***" + copy.substring(endIndex);
                        // reimport - expensive (makes a new TextFlow) and probably looses some fidelity (styles, etc)
                        texty.textFlow = TextFlowUtil.importFromString(copy);
                    }
                }
    
                private function replaceBySplittingFlowElements():void
                {
                    init();
                    if (mgr.hasSelection())
                    {
                        // each call to splitAtPosition leaves the text before startIndex in the original FlowElement
                        // and returns a new FlowElement (TextFlow's in this case) w/the remaining text
                        var textToBeReplaced:FlowElement = flow.splitAtPosition(startIndex);
                        // chops out whatever was between start/end indexes
                        var remainingTextToAddBack:TextFlow = textToBeReplaced.splitAtPosition(endIndex - startIndex) as TextFlow;
                        // insert replacment text
                        var span:SpanElement = new SpanElement();
                        span.text = "Hi, I'm a replacement String.";
                        // assumes last element is a paragraph but it could be a div :)
                        var lastParagraph:ParagraphElement = flow.getChildAt(flow.numChildren -1) as ParagraphElement;
                        if (lastParagraph)
                        {
                            lastParagraph.addChild(span);
                            // merge the last paragraph w/the 1st paragraph of remaining text
                            var firstP:ParagraphElement = remainingTextToAddBack.getChildAt(0) as ParagraphElement;
                            if (firstP)
                            {
                                lastParagraph.replaceChildren( lastParagraph.numChildren, lastParagraph.numChildren, getParagraphChildren(firstP) );
                            }
    
                        }
    
                    }
                }
    
                private function getParagraphChildren(p:ParagraphElement):Array
                {
                    var kids:Array =[];
                    var numKids:int = p.numChildren;
                    for (var i:int = 0; i<numKids; i++)
                    {
                        kids.push( p.getChildAt(i) );
                    }
                    return kids;
                }
    
            ]]>
        </fx:Script>
    
        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
    
        <s:HGroup>
            <s:Button label="Replace by importing text" click="replaceByImportText()" />
            <s:Button label="Replace by using FlowElements" click="replaceBySplittingFlowElements()"/>
        </s:HGroup>
    
    
        <s:TextArea id="texty" top="40" left="40" right="40" bottom="40">
            <s:p>
                <s:span>
                    <s:text>
                        Bacon ipsum dolor sit amet turducken pancetta short ribs tail anim pig.
                        In pastrami in, ball tip flank shankle beef ribs spare ribs deserunt pancetta esse cupidatat aliquip venison pork chop.
                        Pork loin commodo corned beef ullamco culpa dolore occaecat, capicola adipisicing ribeye bresaola sunt est. 
                        Commodo labore culpa ut, sausage ad meatloaf adipisicing.
                    </s:text>
                </s:span>
                <s:br/>
            </s:p>
    
            <s:p>
                <s:span>
                    <s:text>
                        Quis qui aliqua enim, rump jerky aute bresaola aliquip pig speck short loin.
                        Non ea eiusmod shoulder, consequat enim ribeye sed. Meatloaf tenderloin pork loin reprehenderit.
                        Enim rump eiusmod, tri-tip capicola in do frankfurter dolore.
                        Culpa elit meatball pariatur turducken, leberkas excepteur irure in pork belly shank consequat.
                        Sint biltong t-bone veniam shankle. Consectetur irure et minim.
                    </s:text>
                </s:span>
                <s:br/>
            </s:p>
    
            <s:p>
                <s:span>
                    <s:text>
                        Excepteur laborum non corned beef, est dolore pastrami jowl id.
                        Leberkas short ribs ham, tempor id sed esse. Officia eiusmod pork frankfurter leberkas cow, nisi qui filet mignon mollit swine bacon veniam.
                        Nostrud deserunt nulla ground round, shankle sausage aliqua ut frankfurter culpa.
                        Veniam hamburger spare ribs, ullamco dolor labore salami capicola short loin swine.
                    </s:text>
                </s:span>
            </s:p>
    
        </s:TextArea>
    </s:Application>
    
    
    
    我想贡献一些代码,因为我从这里创建了我的代码,而且几乎没有关于“splitAtPosition”方法的示例或文档

    此代码将在textflow(文本流)中的任何位置(索引)插入FlowElement(FlowElement)-请记住运行
    textflow.flowComposer.UpdateAllController()运行函数后:

    private function insertFlowElementInTextFlowAt(textflow:TextFlow,index:int,flowElement:FlowElement):void{
    
        var part2:TextFlow = textflow.splitAtPosition(index) as TextFlow;
        var part2_firstPara:ParagraphElement = part2.getChildAt(0) as ParagraphElement;     
        var textflow_lastPara:ParagraphElement = textflow.getChildAt(textflow.numChildren -1) as ParagraphElement;  
    
    
    
        if (textflow.textLength == index){
    
            part2_firstPara.addChildAt(0,flowElement);
            textflow.replaceChildren(textflow.numChildren,textflow.numChildren,getFlowElementChildren(part2,0))
    
        }else if(index < textflow.textLength){
    
            textflow_lastPara.addChild(flowElement);
            textflow_lastPara.replaceChildren( textflow_lastPara.numChildren, textflow_lastPara.numChildren, getFlowElementChildren(part2_firstPara));
            textflow.replaceChildren(textflow.numChildren,textflow.numChildren,getFlowElementChildren(part2,1))
    
        }
    
    
    
    }
    
    private function getFlowElementChildren(p:FlowGroupElement,start:int=0):Array
    {
        var kids:Array = new Array();
        for (var i:int = start; i<p.numChildren; i++){
            kids.push( p.getChildAt(i) );
        }
        return kids;
    }
    
    private函数insertFlowElementInTextFlowAt(textflow:textflow,index:int,flowElement:flowElement):void{
    var part2:TextFlow=TextFlow.splitAtPosition(索引)作为TextFlow;
    var part2_firstPara:ParagraphElement=part2.getChildAt(0)作为ParagraphElement;
    var textflow_lastPara:ParagraphElement=textflow.getChildAt(textflow.numChildren-1)作为ParagraphElement;
    if(textflow.textLength==索引){
    第2部分第一段添加Childat(0,flowElement);
    textflow.replaceChildren(textflow.numChildren,textflow.numChildren,getFlowElementChildren(part2,0))
    }else if(索引对于(var i:int=start;i我想贡献一些代码,因为我从这里创建了我的代码,而且几乎没有关于“splitAtPosition”方法的示例或文档

    此代码将在textflow(文本流)中的任何位置(索引)插入FlowElement(FlowElement)-请记住在运行函数后运行
    textflow.flowComposer.UpdateAllController();

    private function insertFlowElementInTextFlowAt(textflow:TextFlow,index:int,flowElement:FlowElement):void{
    
        var part2:TextFlow = textflow.splitAtPosition(index) as TextFlow;
        var part2_firstPara:ParagraphElement = part2.getChildAt(0) as ParagraphElement;     
        var textflow_lastPara:ParagraphElement = textflow.getChildAt(textflow.numChildren -1) as ParagraphElement;  
    
    
    
        if (textflow.textLength == index){
    
            part2_firstPara.addChildAt(0,flowElement);
            textflow.replaceChildren(textflow.numChildren,textflow.numChildren,getFlowElementChildren(part2,0))
    
        }else if(index < textflow.textLength){
    
            textflow_lastPara.addChild(flowElement);
            textflow_lastPara.replaceChildren( textflow_lastPara.numChildren, textflow_lastPara.numChildren, getFlowElementChildren(part2_firstPara));
            textflow.replaceChildren(textflow.numChildren,textflow.numChildren,getFlowElementChildren(part2,1))
    
        }
    
    
    
    }
    
    private function getFlowElementChildren(p:FlowGroupElement,start:int=0):Array
    {
        var kids:Array = new Array();
        for (var i:int = start; i<p.numChildren; i++){
            kids.push( p.getChildAt(i) );
        }
        return kids;
    }
    
    private函数insertFlowElementInTextFlowAt(textflow:textflow,index:int,flowElement:flowElement):void{
    var part2:TextFlow=TextFlow.splitAtPosition(索引)作为TextFlow;
    var part2_firstPara:ParagraphElement=part2.getChildAt(0)作为ParagraphElement;
    var textflow_lastPara:ParagraphElement=textflow.getChildAt(textflow.numChildren-1)作为ParagraphElement;
    if(textflow.textLength==索引){
    第2部分第一段添加Childat(0,flowElement);
    textflow.replaceChildren(textflow.numChildren,textflow.numChildren,getFlowElementChildren(part2,0))
    }else if(索引