O'Reilly logo

ActionScript 3.0 Cookbook by Joey Lott, Darron Schall, Keith Peters

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Formatting Text

Problem

You want to format the text in a text field.

Solution

Use HTML tags, pass a TextFormat object to the TextField.setTextFormat() method, or use a StyleSheet object, and apply it to the text field’s styleSheet property.

Discussion

Although you can set the color of the entire contents of the text field using the textColor property, for example, the TextField class doesn’t offer precise control over character formatting. However, there are several ways in which you can apply more exacting formatting to text fields:

  • Use HTML tags to apply formatting. For example, you can use <font>, <b>, and <u> tags.

  • Use a TextFormat object.

  • Use CSS.

Each of the three ways in which you can apply formatting to a text field has its own advantages and disadvantages. Applying HTML formatting is relatively simple, but it is the most difficult to manage of the different techniques. Using a TextFormat object is the more complex than applying formatting via HTML. However, when you want to apply complex formatting, it is a better option than HTML formatting. Using CSS with a StyleSheet object allows the greatest flexibility, and it allows you to load a CSS document, which makes it simple to maintain because you can edit the CSS document without having to re-export the .swf file.

For fast and simple formatting, HTML tags are simplest. For example, the following code displays bolded and underlined text:

field.html = true;
field.htmlText = "<b>Bold text</b> <u>Underlined text</u>";

You can use ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required