Making <p> and <br> Tags Work with htmlText in ActionScript 3.0
I had one hell of a time trying to figure out why my paragraph breaks (<p>) and line breaks (<br>) were not working when I was using htmlText in Flash CS3 with ActionScript 3.0. All of the other HTML tags that are allowed by Flash worked fine, but those two break tags just would do anything. I had created a TextField variable called “info” and all the code checked out but there was one very important thing that I needed to put in place before it would all work the way I needed it to.
Here is the code that wouldn’t work:
var info:TextField = new TextField(); info.htmlText = "First line. <br> Second line. <br> Third line."; addChild(info); |
Here is what is should have looked like:
var info:TextField = new TextField(); info.multiline = true; info.htmlText = "First line. <br> Second line. <br> Third line."; addChild(info); |
Why wasn’t my original code working? Well, to make sure that the HTML tags for paragraph breaks and line breaks would function properly, I needed to set the TextField’s multiline parameter to “true”. Just one tiny line of code and then everything worked perfectly. I hate it when that happens!



Thanks for that – that was driving me nuts! The text API in AS3 is pretty confusing really, what with html, textFormat and styleSheets all interacting in unpredictable ways.
Ohhhhhh yeah! Thank you very much!
Thank god I found your article. Was driving me nuts, too.
I also discovered that the multiline property has to be set BEFORE you set the htmlProperty, otherwise the P and BR tags will still not work.
Thanks, that fixed it for me.
Thanks this article and your answer very much
This made my day!
Thank you thank you thank you thank you thank you!!!! Wow, wasted so much time trying to figure that one out. Much appreciated!
You Rock!
OMG i <3 u
damn, i was going crazy.. thanks for posting. i still hate as3!
Thanks for your post on this topic. You probably just saved me 30 minutes of digging around the Flash Docs.
great, this was really where I was looking for! lame that the appendText method is not working for htmlText as well. Anyways, thanks for the post, you really helped me out!
I would really appreciate some help!
I’m trying to use the TextArea component with a slider and a numeric stepper to display some text. I thought perhaps allowing the text to be enlarged would be useful for those with bad eyes, but I don’t know how to format the text (paragraphs, bold headings, regular body text …etc.)
Initially when the TextArea comes up, it does not have the font or size that it is supposed to. The values for the slider and stepper are the same as what is in the code.
This is the code I’m using:
I hope you can help me out!
Thank you!!!
Hey Karen,
Hmm. That’s a tough one. Is the font embedded?
I’m not sure how to deal with embedding fonts and making them work with the code I am using. Embedding fonts is still new to me. I did figure out how to add the fonts to my library – is that what you mean? I just don’t understand how to make it work with components.
I hate not being able to figure something out, so I do appreciate the help a great deal.
Take a look at this.
http://www.adobe.com/devnet/flash/quickstart/embedding_fonts/
Thanks!! One less gray hair.
Ah Details, Details…
After my own trials and tribulations with the break tag – Just wanted to also point out that if you are importing node text from an xml document into a text area as htmlText, and you want the break tag in particular to be recognized, you have to escape the braces in the xml node so it looks like &lb;br&rb;
The xml document wont let you get away without a closing break, and flash wont recognize the self closing break you would usually use.
Live and learn!
Bah! My mind did not code complete that correctly. The properly escaped tag is <br>
Make that
Thanks! This was killing me for over an hour!
Grrrrrrrreaaaa! Thanks a lot. It was driving me crazy!
Remember to set your “multiline” property to true before you list your “htmlText”. This will result in the most accurate html formatting. (At least I notice a difference when I’m using the tag.)
using the list item tage ().
Ohhh very nice! Thank you so much! I was wondering why the and tags weren’t working!
Your solution fixed my code, thank you!
great tip.
Thanks! that was driving me crazy for a couple of hours. Also beware to heed the reminder about moving the multiline property before you set the htmlProperty. That fixed it!!!!!!
Thank you!
Cool, thank you!
Thanks, that saved me..
Thanks a lot, setting multiline parameter to true helped.
Thank you.