You can apply formatting to text using the TextFormat class, which defines many properties that allow you to set everything from font family and font size to kerning and color. The Flash Help documentation lists all the properties of the TextFormat class, and it is an excellent reference for details of each setting. However, regardless of which properties you want to set, you'll construct the new TextFormat and apply it in the same ways.
When you want to construct a TextFormat object, you can use the constructor as part of a new statement, as the following code illustrates:
var formatter:TextFormat = new TextFormat();
After you construct a TextFormat object, you can apply settings. The following sets the font and size properties:
formatter.font = "_sans"; formatter.size = 20;
After you set the properties, you can apply the formatting to a text field with the setTextFormat() method of the TextField class. The method allows you to apply a TextFormat object to the entire text field. The following code illustrates by applying a TextFormat object called formatter to the text of a text field called exampleField:
When you apply formatting to a text field with the setTextFormat() method, it applies the current TextFormat settings to the current text of the text field. If you update the text after applying the formatting, you'll have to reapply the formatting. And if you change the settings of the TextFormat object, you'll have to reapply it if you want it to change the formatting of the text field.
You can optionally apply the formatting to just a specific substring of the text by specifying the starting and stopping indices of that substring as parameters for the setTextFormat() method.
exampleField.setTextFormat(formatter, 10, 20);
In the next task, you'll add a message when the user wins the game. The message uses a TextFormat object.