JSFL:将文本从文本字段转换为 HTML 格式字符串

发布于 2024-12-05 02:04:31 字数 668 浏览 1 评论 0原文

我有一个看似简单的问题:如何从文本字段获取文本并包含格式?通过查看通常的文档,我发现只能获取文本。也可以获取文本格式,但这仅在整个文本字段仅使用一种格式时才有效。我需要精确的格式,以便将其转换为带有 h​​tml 标签的字符串。

我个人需要这个,这样我就可以将它传递给使用 HTML 进行格式化的定制文本字段组件。但它也可以用于简单地将任何文本字段的内容导出为任何其他格式。其他人也可能对此感兴趣。 在其他地方寻找解决方案时,我发现了这个:

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

这似乎与我需要的相反,将 HTML 转换为 Flash 文本。到目前为止,我自己扭转这一局面的尝试尚未成功。也许其他人看到了一种简单的方法来扭转我所缺少的?可能还有其他解决方案。一种可能是获取文本字段的精确数据,其中应包括某种格式标记(XML,当查看存储的 FLA 文件的内容时)。然后删除/转换这些标签。但如果可能的话,我不知道该怎么做。另一种选择是使用 start- 和 endIndex 循环遍历每个字符,并将每种格式类型存储在数组中。然后我可以将格式应用于每个字符。但这会导致标签过多。特别是对于超链接!那么有人可以帮我解决这个问题吗?

I've got a deceptively simple question: how can I get the text 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏の忆 2024-12-12 02:04:31

有点晚了,但以下函数将 JSFL 静态文本元素作为输入,并根据在其 TextRuns 中找到的样式返回 HTML 字符串(使用 Flash 友好的 标记)大批。它执行一些基本的正则表达式来清除一些标签和双空格等,并将 /r/n 转换为
标签。它可能并不完美,但希望您可以轻松地了解发生了什么,从而更改或修复它。

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文