Using the TextFormat class 137
Creating text fields at runtime
You can use the createTextField() method of the MovieClip class to create an empty text field
on the Stage at runtime. The new text field is attached to the Timeline of the movie clip that calls
the method. The
createTextField() method uses the following syntax:
movieClip.createTextField(instanceName, depth, x, y, width, height)
For example, the following code creates a 300 x 100 pixel text field named test_txt at point
(0,0) and a depth (z-order) of 10.
_root.createTextField("test_txt", 10, 0, 0, 300, 100);
You use the instance name specified in the createTextField() call to access the methods and
properties of the TextField class. For example, the following code creates a new text field named
test_txt, and then modifies its properties to make it a multiline, word-wrapping text field that
expands to fit inserted text. Lastly, it assigns some text to the text field’s
text property.
_root.createTextField("test_txt", 10, 0, 0, 100, 50);
test_txt.multiline = true;
test_txt.wordWrap = true;
test_txt.autoSize = true;
test_txt.text = "Create new text fields with the MovieClip.createTextField
method.";
You can use the TextField.removeTextField() method to remove a text field created with
createTextField(). The removeTextField() method does not work on a text field placed by
the Timeline during authoring.
For more information, see
MovieClip.createTextField() on page 494 and
TextField.removeTextField() on page 698.
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 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 that 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() on page 702 and TextField.setNewTextFormat()
on page 701.