Sunday, August 1, 2010

Using HTML in Flash text fields (AS3)

Did you know that you can apply HTML formatting to dynamic textfields in Flash? In this tutorial you will learn how!

First create a dynamic text field on your screen. It can be done through AS3 or manually (text tool), it doesn't matter. Make sure it has an instance name of myTextfield.

Now let's try bolding some text:
myTextfield.text="Greetings from <b>kirill</b>!"

The result will be:

Greetings from <b>kirill</b>!

Not exactly what we wanted. The textfield returned our text exactly as it is - as a string. To enable HTML formatting, we need to tell flash that we're going to use HTML. We do that by putting htmlText instead of text:

myTextfield.htmlText="Greetings from <b>kirill</b>!"

Result:

Greetings from kirill!

Here's a list of tags that Flash supports: <a> (attributes: href, target), <b>, <br>, <font> (attributes: color, face, size), <img> (attributes: src, width, height, align, hspace, vspace, id, checkPolicyFile), <i>, <li>, <p> (attributes: align, class), <span> (attribute: class), <textformat> (attributes: blockindent, indent, leading, leftmargin, rightmargin, tabstops), <u>.

Remember that when adding a link, flash won't make it different than text (blue underlined text) but you can do that with <u> and <font> tags, or with CSS. I will explain how to use CSS in flash in future.

Thanks for reading!

No comments:

Post a Comment