我不知道如何标题,所以很抱歉。如果您有更好的标题建议,请告诉我,我会更改它。
我有一大块文本在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?
大多数标签都是基本的,旨在显示我可以做的格式化方式。但是,由于Adobe 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&&&&
,但这似乎是一个警察,如果这些标签中有多个位于文本块中,我仍会遇到问题标签被删除,然后是自动换行,以一种非常不可预测的方式移动线条。
答案 0 :(得分:1)
您是否尝试过使用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
}
答案 1 :(得分:1)
myTextField.textHeight告诉您文本的高度(以像素为单位)。因此,您可以将字符串拆分为您正在查找的内容,将文本放在textField中的目标之前并获取textHeight,然后将其余文本放入。
这是一些示例代码 - tMain是textField的名称:
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像素数。
让我知道你有任何问题。
答案 2 :(得分:1)
另一种方法是在您的html字符串中搜索<showImage ..>
代码并将其替换为短代码,例如[showImage ..]
,,然后将htmlString
插入{{1} }}。那么这不是xml而是文本,你可以检索textField
值(即如果我理解你的问题)。
然后你的其余代码可以从那里获取它。
(ps使用y
似乎是不错的选择)