Erik’s Brain

The reward of a thing well done...

ActionScript TextFormat.font Values

One thing I have to do a lot with ActionScript 3 is make TextField instances at run time that use something other than the default font. One way that I’ll do that is like this:

1
2
3
4
5
6
7
// make a new TextFormat object and set its "font" property
var myFormat:TextFormat = new TextFormat();
myFormat.font = "Arial";

// then make the TextField and apply the TextFormat to it
var tf:TextField = new TextField();
tf.defaultTextFormat = myFormat;

(This doesn’t deal with actually making sure that the font you want to use is embedded in the movie, but that’s another story.)

One part of this that’s been a bit of a mystery to me in some cases is exactly what to set as the TextFormat.font property. And, as you’ll see, there’s good reason to be a little confused, depending on what font you want to use. What you see in the CHARACTER portion of Flash’s PROPERTIES panel doesn’t always match what you need to type in as the TextFormat.font value. For example, if you want to use the following font:

Arial Regular

You need to type the following:

1
myFormat.font = "Arial"

That’s fairly straightforward, I guess. This one is a little less straightforward:

Meta Thin

…and the code you need to type to Flash:

1
myFormat.font = "Meta-Thin"

Where did that hyphen come from that goes betwee “Meta” and “Thin”? And how would I know I need to insert when, if I wanted to use Arial Bold, I’d simply type:

1
myFormat.font = "Arial Bold"

There’s no hyphen in that one. Mysterious.

Even worse, check this one out. Suppose you want to use this specific style of The Sans:

Thesans Sansplain

Guess what name you need to plug into the TextFormat.font property…

1
myFormat.font = "TheSans Plain"

Hmmm… How in the hell would you ever figure that out? And is there any logic here? And where do I find the string to type in for the TextFormat.font property?

The Solution

Turns out, there is an easy way to figure out exactly what to type in for the TextFormat.font property. It’s a bit of a hack, but it gets the job done. The process goes like this:

  1. Make a dynamic text field in the Flash authoring tool and set its font to the font name you are trying to figure out.
  2. Give that dynamic text field an instance name (let’s use myField in this example)
  3. Then type this code into the ActionScript panel:
1
trace(myField.getTextFormat().font)

That’s it. When you test the movie, the Output panel will print out the ActionScript version of the font name, which is exactly what we are looking for.

Comments