html - (AS3) 在自动换行后获取文本字段中的 HTML 特定字符索引

标签 html actionscript-3 parsing air textfield

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

我有一大块文本在 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&&&& , 但这似乎是一种逃避,如果这些标签中有多个在文本 block 中并且标签被删除,然后以一种非常不可预测的方式换行,我仍然会遇到问题。

最佳答案

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 - (AS3) 在自动换行后获取文本字段中的 HTML 特定字符索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32998656/

相关文章:

flash - 使用 renderMode=GPU 的 iPad AIR 应用程序的性能突然下降

python - 在 Python 中解析嵌套 Json 以删除列中的特殊字符

xml - 在 Windows 上使用包 XML 时发生内存泄漏

java - 如何为用户输入(单词)分配一个要在代码中使用的整数值

php - 使用后期处理上传进度

javascript - 更改指令的范围值

flash - AS3,NetConnection/NetStream : Is there a smart way to split source url?

java - 将对象从flex发送到java servlet

javascript - jQuery SerializeArray — 如何排除带有空 <opiton> 值的 <select>?

javascript - 如何在视频 html5 上回退到 Gif 但不事先下载 gif?