User Guide

224 Chapter 9: Working with Text
Using the TextFormat class
You can use the ActionScript TextFormat class to set formatting properties of a text field. The
TextFormat class incorporates character and paragraph formatting information. Character
formatting information describes the appearance of individual characters: font name, point size,
color, and an associated URL. Paragraph formatting information describes the appearance of a
paragraph: left margin, right margin, indentation of the first line, and left, right, or
center alignment.
To use the TextFormat class, you first create a TextFormat object and set its character and
paragraph formatting styles. You then apply the TextFormat object to a text field using the
TextField.setTextFormat() or TextField.setNewTextFormat() methods.
The
setTextFormat() method changes the text format that is applied to individual characters,
to groups of characters, or to the entire body of text in a text field. Newly inserted text,
however—such as text entered by a user or inserted with ActionScript—does not assume the
formatting specified by a
setTextFormat() call. To specify the default formatting for newly
inserted text, use
TextField.setNewTextFormat(). For more information, see
TextField.setTextFormat() and TextField.setNewTextFormat() in Flash ActionScript
Language Reference.
To format a text field with the TextFormat class:
1.
In a new Macromedia Flash document, create a text field on the Stage using the Text tool. Type
some text in the text field on the Stage, such as
“Bold, italic, 24 point text”.
2.
In the Property inspector, type myText_txt in the Instance Name text box, select Dynamic
from the Text Type pop-up menu, and select Multiline from the Line Type pop-up menu.
3.
In the Timeline, select the first frame in Layer 1, and open the Actions panel (Window >
Development Panels > Actions).
4.
Enter the following code in the Actions panel to create a TextFormat object, set the bold and
italic properties to true, and the size property to 24:
// Create a TextFormat object
var txtfmt:TextFormat = new TextFormat();
// Specify paragraph and character formatting
txtfmt.bold = true;
txtfmt.italic = true;
txtfmt.size = 24;