(AS3) 换行后在文本字段中获取 HTML-specific 字符索引

(AS3) Getting an HTML-specific character index in a textfield after word wrap

我不知道如何表达标题,很抱歉。如果您有更好的标题建议,请告诉我,我会更改。

我有一大块文本在 TextField 中显示为 HTML。此文本的示例如下:

1
<font size="30" color="#FF0000">When your only tool is a hammer, all problems start looking like nails.</font>
</br>
2
<i>99 percent of lawyers give the rest a bad name.</i>
<b>Artificial intelligence is no match for natural stupidity.</b>
<u>The last thing I want to do is insult you. But it IS on the list.</u>
</br>
3<showimage=Images/image1.jpg>
I don't have a solution, but I do admire the problem.
The only substitute for good manners is fast reflexes.
Support bacteria - they're the only culture some people have.
</br>
4
Letting the cat out of the bag is a whole lot easier than putting it back in.
Well, here I am! What are your other two wishes?

大多数标签都是基本的,旨在显示我可以明智地进行格式化。但是,由于 Adob​​e Air 有一个沙盒可以防止内联图像(通过 <img src='foo.png'> 标签),我不得不想出另一种显示图像的方法。

基本上,我打算在屏幕上的某个位置显示图像,并且当用户滚动时,图像将根据他们滚动到文本中的位置而改变。图片可以是背景图片,右边的幻灯片,任何东西。

在上面的代码片段中,查找我的自定义标签 <showimage=Images/image1.jpg>。一旦 TextField 呈现为 HTML 并换行,我想获得该标签的本地 y 位置。问题是,当我查询标签的 y 位置(使用 getCharBoundaries)时,我只能在将文本呈现为 .text 而不是 .htmlText 时搜索标签.如果我在将其呈现为 .htmlText 后在 TextField 中搜索标签,则找不到它,因为标签被隐藏并被格式替换。

我在呈现 HTML 之前得到的 y 值的问题在于,由于字体大小、标签被隐藏以及自动换行改变标签所在的行和 y 值,y 值会有所不同位于.

渲染 HTML 后,如何获得 HTML 标签的正确 y 值?

我考虑过使用不同的样式标签,可能类似于 &&&&&showImage=Images/image1.jpg&&&&,但这看起来像 cop-out,如果有多个这样的标签,我仍然 运行 会遇到问题标签在一段文本中,标签被删除,然后是自动换行,以一种非常不可预测的方式换行。

您是否尝试过使用 HTMLLoader 并使用 loadString 方法,而不是经历解析图像标签的麻烦?这应该将所有内容加载到适当的位置,包括使用 img 标签的图像。

private var htmlLoader:HTMLLoader;
private function loadHtml(content:String):void
{
    htmlLoader = new HTMLLoader(); //Constructor
    htmlLoader.addEventListener(Event.COMPLETE, handleHtmlLoadComplete); //Handle complete
    htmlLoader.loadString(content); //Load html from string
}

private function handleHtmlLoadComplete(e:Event):void 
{
    htmlLoader.removeEventListener(Event.COMPLETE, handleHtmlLoadComplete); //Always remove event listeners!
    htmlLoader.width = htmlLoader.contentWidth; //Set width and height container
    htmlLoader.height = htmlLoader.contentHeight;
    addChild(htmlLoader); //Add to stage
}

myTextField.textHeight 告诉您文本的高度(以像素为单位)。因此,您可以根据要查找的任何内容拆分字符串,将文本放在文本字段中的目标之前并获取文本高度,然后将其余文本放入。

这是一些示例代码 - tMain 是文本字段的名称:

var iTextHeight: int = 0;
var sText: String = '<font size="30" color="#FF0000">When your only tool is a hammer, all problems start looking like nails.</font></br><i>99 percent of lawyers give the rest a bad name.</i><b>Artificial intelligence is no match for natural stupidity.</b><u>The last thing I want to do is insult you. But it IS on the list.</u></br><showimage=Images/image1.jpg> I don\'t have a solution, but I do admire the problem. The only substitute for good manners is fast reflexes. Support bacteria - they\'re the only culture some people have. </br>Letting the cat out of the bag is a whole lot easier than putting it back in. Well, here I am! What are your other two wishes?';
var aStringParts: Array = sText.split("<showimage=Images/image1.jpg>");
for (var i = 0; i < aStringParts.length; i++) {
    if (i == 0) {
        tMain.htmlText = aStringParts[i];
        trace("height of text: " + tMain.textHeight);
    } else {
        tMain.appendText(aStringParts[i]);
    }
}

sText 在您要查找的标签上拆分(删除您要查找的文本并将剩余的文本拆分为一个数组)。标签前的文本放在 textField 中,并跟踪 textHeight。然后将其余文本放入 textField 中。这为您提供了安排事物所需的 y 像素数。

如有任何问题,请告诉我。

另一种方法是在 html 字符串中搜索 <showImage ..> 标签并用短代码替换这些标签,例如 [showImage ..]before 插入 htmlStringtextField 中。那么这不是 xml 而是文本,您可以检索 y 值(也就是说,如果我正确理解您的问题)。

然后您的其余代码可以从那里获取它。

(ps 使用 HtmlLoader 似乎是不错的选择)