开发者

Flex does not handle line breaks in htmlText

I store the htmlText property of a RichTextEditor in the database.

I retrieve it in another instance and I want to show the user the first line of it as plain text

So I let Flex handle the conversion by using a function like this

var editor:TextField = new TextField();
editor.htmlText = htmlTextFromDb;
var converted:String = editor.text;

However, the issue is that this conversion does not handle lines properly. I get everything in one line!

Lets say what I get from the database is this

<TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Arial" 开发者_运维技巧SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">This is line one</FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Arial" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">This is line two</FONT></P></TEXTFORMAT>

As soon as I say editor.htmlText = htmlTextFromDb, editor.htmlText becomes

<TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Arial" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">This is line oneThis is line two</FONT></P></TEXTFORMAT>

It acts as if new lines are not present.

How do I solve this?


I would think about server side conversion of this HTML text.

But in AS only you could do it line by line by splitting on line break, then converting the line to text and after this concatenating all. A bit ugly, but should work.

var editor:TextField = new TextField();
var result:Array = new Array();
var input:Array = htmlTextFromDb.split(/\n/);
for each( var line:String in input) {
    editor.htmlText = line;
    result.push(editor.text);
}
var converted:String = result.join("\n");

edit

Replacing the tags with regular expression would be another way. First replace the closing paragraph </P> with a line break, and then remove all remaining tags.

var lnRegExp:RegExp = new RegExp("</P>", "g");
s = s.replace(lnRegExp, "\n");
var tagRegExp:RegExp = new RegExp("<([^>\\s]+)(\\s[^>]+)*>", "g");
trace(s.replace(tagRegExp, ""));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜