O'Reilly logo

Actionscript Cookbook by Joey Lott

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

8.16. Formatting Existing Text

Problem

You want to format the existing text in a text field.

Solution

Pass a TextFormat object to the TextField.setTextFormat( ) method.

Discussion

A text field allows minimal control over the formatting of the text it displays, such as by setting the htmlText property. 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, the TextFormat class is a “helper” class; TextFormat objects can format the text displayed in text fields.

The first step in formatting text is to create a TextFormat object using the constructor method:

myTextFormat = new TextFormat(  );

Next, assign values to the TextFormat object’s properties as you desire:

myTextFormat.align = "center";  // Center-align the text.
myTextFormat.bold = true;       // Bold the text.
myTextFormat.color = 0xFFFF00;  // Make the text yellow.
myTextFormat.blockIndent = 5;   // Adjust the margin by five pixels.

You can apply text formatting to the existing text for an entire text field by passing a TextFormat object to the text field’s setTextFormat( ) method:

myTextField.setTextFormat(myTextFormat);

When you invoke the setTextFormat( ) method in this way, the formatting from the TextFormat object is applied to the text already assigned to the text field. The formatting does not apply to any text assigned to the text field after the setTextFormat( ) method is invoked. If additional ...

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