TLF的Adobe用Flex / AS3:如何以编程方式更改\ N的文本字符串转换成段落标记,并插入到TextFlow中? [英] Adobe TLF with Flex/AS3: how to programmatically change \n in text string into paragraph tags and insert into TextFlow?
问题描述
我的Flex / AS3 Web应用程序访问数据库和检索包含\ n表示换行字符的文本。否则,文本看起来像普通的文本。
My Flex/AS3 web application accesses a database and retrieves text containing \n for newline character. Otherwise the text looks like normal text.
有没有一种方法来转换成\ n为段落标记,我可以插入到点火的TextFlow?
Is there a way to convert the \n into paragraph tags that I can insert into an spark TextFlow?
例如,我和TLF唯一的经验是在编写MXML code,如如下:
For example, my only experience with TLF is in writing mxml code such as follows:
<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>
假设我检索一个字符串从数据库中,如:
Suppose I retrieve a string from the database such as:
paragraph1_text =这是一些文本。\ n这是一些文字。\ n这更是文字。
即使插入该到code以上会识别新的行字符,它不会承认的 paragraphSpaceAfter =0.3
。我的目标是有效果:
Even though inserting this into the code above will recognize the new line character, it won't recognize the paragraphSpaceAfter="0.3"
. My goal is to have the effect of:
<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>
有没有什么办法来实现这样的编程方式(数据库中的文本可以是任何东西)?
Is there any way to achieve something like this programmatically (the database text could be anything)?
我试图改变paragraph1_text为:
I tried changing paragraph1_text to:
paragraph1_text =&LT; S:P&gt;这是一些文字&lt; / S:P&GT;&LT; S:P&gt;这是一些文字&lt; / S:P&GT;&LT ; S:P&gt;这是更文字&lt; / S:P&gt;中
,然后使用这个第一code以上的块,但它只是打印出所有的&LT; S:P&GT;
和&LT; / S:P&GT;
标签
and then using this in the first code block above, but it simply printed out all of the <s:p>
and </s:p>
tags.
任何意见AP preciated。
Any advice appreciated.
推荐答案
您可以使用ActionScript实现这一目标。比方说,你的名字你的TextFlow像
You can achieve this by using ActionScript. Let's say you name your TextFlow like
<s:TextFlow id="myFlow">
您的ActionScript code座可能是这样的:
Your ActionScript code block could look like:
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,文字,2);
In your case, since you want to add the paragraphs starting as the 3rd element, once the text is retrieved, you can call insertParagaph( myFlow, text, 2 );
这篇关于TLF的Adobe用Flex / AS3:如何以编程方式更改\ N的文本字符串转换成段落标记,并插入到TextFlow中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!