Adobe TLF with Flex / AS3:如何以编程方式将文本字符串中的\ n更改为段落标记并插入到TextFlow中?

时间:2012-07-13 17:20:03

标签: actionscript-3 flex tlf

我的Flex / AS3 Web应用程序访问数据库并检索包含\ n的文本以获取换行符。否则,文本看起来像普通文本。

有没有办法将\ n转换为可以插入到Spark TextFlow中的段落标记?

例如,我对TLF的唯一体验是编写mxml代码,如下所示:

<s:RichEditableText width="100%" height="100%"
                    textAlign="left" paddingTop="0"
                    paragraphSpaceAfter="0.3"
                    editable="false">
<s:textFlow>
  <s:TextFlow>
    <s:p fontWeight="bold" fontSize="15">My Title Text</s:p>
    <s:p fontSize="2"/>
    <s:p>{paragraph1_text}</s:p>
    <s:p>{paragraph2_text}</s:p>
    <s:p>{paragraph3_text}</s:p>
    <s:p fontSize="2"/>
  </s:TextFlow>
</s:textFlow>
</s:RichEditableText>

假设我从数据库中检索字符串,例如:

paragraph1_text = "This is some text.\nThis is some more text.\nThis is even more text."

即使将其插入上面的代码中,也会识别新的行字符,但它无法识别paragraphSpaceAfter="0.3"。我的目标是产生以下效果:

<s:RichEditableText width="100%" height="100%"
                    textAlign="left" paddingTop="0"
                    paragraphSpaceAfter="0.3"
                    editable="false">
<s:textFlow>
  <s:TextFlow>
    <s:p fontWeight="bold" fontSize="15">My Title Text</s:p>
    <s:p fontSize="2"/>
    <s:p>This is some text.</s:p>
    <s:p>This is some more text.</s:p>
    <s:p>This is even more text.</s:p>
    <s:p>{paragraph2_text}</s:p>
    <s:p>{paragraph3_text}</s:p>
  <s:p fontSize="2"/>
</s:TextFlow>

  

有没有办法以编程方式实现这样的东西(数据库文本可以是任何东西)?

我尝试将paragraph1_text更改为:

paragraph1_text="<s:p>This is some text.</s:p><s:p>This is some more text.</s:p><s:p>This is even more text.</s:p>"

然后在上面的第一个代码块中使用它,但它只打印出所有<s:p></s:p>标记。

任何建议表示赞赏。

1 个答案:

答案 0 :(得分:2)

您可以使用ActionScript实现此目的。假设您将TextFlow命名为

<s:TextFlow id="myFlow">

您的ActionScript代码块可能如下所示:

import flashx.textLayout.elements.*;

public function insertParagraph( textFlow:TextFlow, text:String, locationIndex:uint ):void
{
    var paragraphs:Vector.<ParagraphElement> = getParagraphElements( text );
    for ( var i:uint = 0; i < paragraphs.length; i++ )
    {
        textFlow.addChildAt( paragraphs[i], locationIndex + i );
    }
}

public function getParagraphElements( text:String ):Vector.<ParagraphElement>
{
    var textParagraphs:Array = text.split("\n");
    var result:Vector.<ParagraphElement> = new Vector.<ParagraphElement>();
    for ( var i:uint = 0; i < textParagraphs.length; i++ )
    {
        var p:ParagraphElement = new ParagraphElement();
        var span:SpanElement = new SpanElement();
        span.text = textParagraphs[i];
        p.addChild(span);
        result.push(p);
    }

    return result;
}

在您的情况下,由于您要添加从第3个元素开始的段落,一旦检索到文本,您可以调用insertParagaph(myFlow,text,2);