开发者

How to replace span element text in textflow with new value

开发者 https://www.devze.com 2023-03-19 11:00 出处:网络
I have textflow with name mytextflow and after I wrote text I want to replace the text with another one in array. I use this code, it writes but replaces the line with the second element in array (eg.

I have textflow with name mytextflow and after I wrote text I want to replace the text with another one in array. I use this code, it writes but replaces the line with the second element in array (eg. line开发者_Python百科 1 with second element in the array and so on).

for ( var ii:int = start_index  ; ii <= end_index ; ii++)
{
    add_kashida(ii);
}

function add_kashida(ii)
{
    var leafn:SpanElement = new SpanElement();                
    var p:ParagraphElement = new ParagraphElement();

    leafn = SpanElement(mytextflow.findLeaf(ii));
    p = leafn.getParagraph();

    leafn.text=line_text[ii].toString();

    p.addChild(leafn);

    mytextflow.flowComposer.updateAllControllers();
}

Please help me.


Your question is a bit hard to understand, but this is what I make of it: you have a fixed number of SpanElements and you wish to replace the text of each Span with a text that is in an Array (that has a length equal to the number of Spans). Please correct me if I'm wrong.

Furthermore, from your code I assume that each Span has its own Paragraph. A little something like this:

<s:TextFlow id="mytextflow">
    <s:p><s:span>old A</s:span></s:p>
    <s:p><s:span>old B</s:span></s:p>
</s:TextFlow>

and you want to replace with the values from e.g.:

var line_text:Array = ["new A", "new B"];

First I'll show you how to do it and then I'll tell you where your code is wrong.

Solution

    var textLines:Array = ["new A", "new B"];

    //we know all children of mytextflow are ParagraphElements
    var paragraphs:Array = mytextflow.mxmlChildren; 
    var numParagraphs:int = paragraphs.length;

    //loop over the ParagraphElements
    for (var i:int=0; i<numParagraphs; i++) {
        //we know the ParagraphElement only contains one SpanElement,
        //so we can get it through getFirstLeaf() and cast it as a SpanElement
        var span:SpanElement = paragraphs[i].getFirstLeaf() as SpanElement;

        //if it really is a SpanElement, we can set its 'text' property
        if (span) span.text = textLines[i];
    }

If your TextFlow object is more compicated than that, there's another way to loop through all the leaf elements, but it's slightly more complicated.

Now for what's wrong with your code:

  • first, you instantiate a new SpanElement and a new ParagraphElement. Then in the next 2 lines you simply overwrite these variables. This is totally unnecessary. It's like writing: var s:String = "a"; s = "b";

  • you try to find the span through findLeaf(ii). The argument that has to be passed into 'findLeaf' is the index of a certain character in the text for which you want to find the SpanElement that contains this character. Not - as you seem to assume - some sort of index of the SpanElement in the TextFlow object.

  • you re-add the Span to the Paragraph. It will not break your code, but it is totally redundant. All you need to do is replace the 'text' property.
  • you call updateAllControllers() upon every pass in the loop. This is also redundant and will cause performance issues with bigger texts. It's enough to call it just once after all the texts have been replaced (and I actually think that you don't need to call it at all since you're not really changing the layout).
0

精彩评论

暂无评论...
验证码 换一张
取 消