Recipe 9.16.
Setting a Text Field's Font
Problem
You want to use ActionScript to change the font
used for some displayed text.
Solution
Use a <font> tag in HTML, set the font property of a
TextFormat object, or use the font-family property
in CSS.
Discussion
You can programmatically specify the font that
is used to display text by using one of several different options.
You can use a <font> tag if you are applying the
formatting using HTML. For example:
field.htmlText = "<font face='Arial'>Formatted text</font>";
You can also use 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:
formatter.font = "Arial";
And you can also define a font-family property
in CSS:
p {
font-family: 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:
formatter.font = "Arial, Verdana, Helvetica";
|
The preceding example uses a TextFormat
object. However, you can specify a comma-delimited list of fonts by
using any of the techniques described.
|
|
The first font is used unless it cannot be
found, in which case the Flash Player attempts to use the next font
in the list.
If none of the specified fonts are found, the
default system font is used. You can optionally specify a
font group. Font groups use
the default device font within a category. A font group is often
preferable to allowing Flash Player to use the default system font,
since at least you'll know what general characteristics the font
will have. There are three font groups: _sans, _serif, and _typewriter. The _sans
group is generally a font such as Arial or Helvetica, the
_serif group is generally a font such as Times or Times New Roman, and the
_typewriter group is generally a font such as Courier or Courier New.
See Also
Recipes 9.13,
9.15,
and 9.17
|