Working with Text > Controlling text with ActionScript > Formatting text dynamically

 

Formatting text dynamically

You can use the ActionScript TextFormat object to set properties of a text field. A TextFormat object 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.

First you must create a new TextFormat object. Then you can use the methods of the TextField object and pass them the TextFormat object as a parameter to format the text in a field.

Each character in a text field may individually be assigned a TextFormat object. The TextFormat object of the first character of a paragraph is examined to perform paragraph formatting for the entire paragraph.

 
To format text dynamically:

1

Choose Window > Actions to open the Actions panel if it isn't already open.

2

Do one of the following to create a text field:

Use the Text tool to drag a text field on the Stage. Assign the text field an instance name in the Property inspector.

For this example, enter the instance name myText.

Use the createTextField method of the MovieClip object. See Creating text. For this example, enter the instance name myText as a parameter of the createTextField method.

3

Do one of the following to place text in the text field:

Enter text into the text field on the Stage.

Set the text property of the TextField object. See Creating text.

4

In the Actions toolbox, select the Objects category, then select the Movie category, then select the Movie category, then select the TextFormat category, then double-click new TextFormat. For this example, enter myformat in the Object parameter field. The following code is displayed in the Script pane:

myformat = new TextFormat();

5

In the Actions toolbox, select the Objects category, then select the Movie category, then select the TextFormat category, then select the Properties category, then double-click color. Repeat this step for the bullet and underline properties. The following code is displayed in the Script pane:

myformat.color = 0xff0000;
myformat.bullet = true;
myformat.underline = true;

6

In the Actions toolbox, select the Objects category, then select the Movie category, then select the TextField category, then select the Method category, then double-click setTextFormat. For this example, enter myText in the Object parameter field.

7

In the Object field, enter the name of the TextFormat object you created in step 4, myformat. The following code displays in the Script pane:

mytext.setTextFormat(myformat);

For a complete list of TextFormat object methods and detailed descriptions of each, see TextFormat (object).