开发者

JSFL: convert text from a textfield to a HTML-format string

I've got a deceptively simple question: how can I get the text开发者_JAVA技巧 from a text field AND include the formatting? Going through the usual docs I found out it is possible to get the text only. It is also possible to get the text formatting, but this only works if the entire text field uses only one kind of formatting. I need the precise formatting so that I convert it to a string with html-tags.

Personally I need this so I can pass it to a custom-made text field component that uses HTML for formatting. But it could also be used to simply export the contents of any text field to any other format. This could be of interest to others out there, too. Looking for a solution elsewhere I found this:

http://labs.thesedays.com/blog/2010/03/18/jsfl-rich-text/

Which seems to do the reverse of what I need, convert HTML to Flash Text. My own attempts to reverse this have not been successful thus far. Maybe someone else sees an easy way to reverse this that I’m missing? There might also be other solutions. One might be to get the EXACT data of the text field, which should include formatting tags of some kind(XML, when looking into the contents of the stored FLA file). Then remove/convert those tags. But I have no idea how to do this, if at all possible. Another option is to cycle through every character using start- and endIndex, and storing each formatting kind in an array. Then I could apply the formatting to each character. But this will result in excess tags. Especially for hyperlinks! So can anybody help me with this?


A bit late to the party but the following function takes a JSFL static text element as input and returns a HTML string (using the Flash-friendly <font> tag) based on the styles found it its TextRuns array. It's doing a bit of basic regex to clear up some tags and double spaces etc. and convert /r and /n to <br/> tags. It's probably not perfect but hopefully you can see what's going on easy enough to change or fix it.

function tfToHTML(p_tf)
{
    var textRuns = p_tf.textRuns;
    var html = "";

    for ( var i=0; i<textRuns.length; i++ )
    {
        var textRun = textRuns[i];
        var chars = textRun.characters;

        chars = chars.replace(/\n/g,"<br/>");
        chars = chars.replace(/\r/g,"<br/>");
        chars = chars.replace(/  /g," ");
        chars = chars.replace(/. <br\/>/g,".<br/>");

        var attrs = textRun.textAttrs;

        var font = attrs.face;
        var size = attrs.size;
        var bold = attrs.bold;
        var italic = attrs.italic;
        var colour = attrs.fillColor;

        if ( bold )
        {
            chars = "<b>"+chars+"</b>";
        }

        if ( italic )
        {
            chars = "<i>"+chars+"</i>";
        }

        chars = "<font size=\""+size+"\" face=\""+font+"\" color=\""+colour+"\">"+chars+"</font>";

        html += chars;
    }

    return html;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜