Flex 3 htmlText calculates height way wrong and strips span tags
I currently have two problems with Flex 3 htmlText.
1) When I am setting my text's htmlText:
myText.htmlText = <html text stored in my mysql database>
It calculates the height way wrong. In most cases, a ton of padding (or whitespace) is added above and below the text. I am not sure what happens, but it calculates the textHeight way higher than normally.
2) If I put <span> tags in my html, it automatically strips them out (instead of just ignoring them). I am using the span tags to be able to dynamically find certain pieces of my text. For instance:
<span class="salutation"&g开发者_如何学运维t;Dear,</span> <span class="tag">[First Name]</span>
is inserted in my htmlText, and I use them to parse out the salutation and tag of my variable data.
Does anyone know of any solutions for these two issues? Any alternative ideas on how to parse out pieces of html? Any way to improve htmlText? How to correctly measure textHeight and/or remove padding?
Thanks!
Basically, the tag needs width and height explicitly set in order for flex to calculate the height correctly on these elements. Also, you can use:
invalidateSize();
validateSize();
To force size validation to occur, which will at times help!
Add these two lines before you use any XML:
XML.ignoreWhitespace = true; XML.prettyPrinting = false;
This should take care of the added lines above and below your text.
ignoreWhitespace strips leading and trailing whitespace from your text nodes.
prettyPrinting, when set to true, formats the XML output, indenting nodes and adding newline characters to make it more readable - but also affecting text output. For example, when you have a structure like this:<p><span class="test">This is a test.</span></p>
Flash will output:
<p> <span class="test"> This is a test. </span> </p>
This leads to your TextField displaying:
(newline) (newline) This is a test. (newline) (newline)
The Flash plugin will remove any HTML tags, if the
styleSheet
property isn't properly set. So in your case, it will remove the<span>
tags and add its own<P>
and<FONT>
tags instead, according to any font information you've specified usingdefaultTextFormat
orsetTextFormat
(try tracing htmlText after you've set it - you'll see). You have to set the TextField'sstyleSheet
property to an actual StyleSheet, if you want to keep your own tags.Parsing out the information you need by using HTML tags is a valid way to go. I usually use numbered placeholders like {0} and
string.replace ()
to insert dynamic values.
精彩评论