8.19. Setting a Text Field’s Font

Problem

You want to use ActionScript to change the font used for some displayed text.

Solution

Set the font property of a TextFormat object and apply the formatting to the text field.

Discussion

You can programmatically specify the font that is used to display text using the font property of a TextFormat object. You can assign to this property the string name of the font (or fonts) that should be used:

myTextFormat.font = "Arial";

The font specified must be available on the computer on which the movie is running. Because some computers may not have the preferred font installed, you can specify multiple font names separated by commas, as follows:

myTextFormat.font = "Arial, Verdana, Helvetica";

The first font is used unless it cannot be found, in which case the Player attempts to use the next font in the list.

Apply the font change to a text field, as follows:

myTextField.setTextFormat(myTextFormat);

If none of the specified fonts are found, the default system font is used.

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.