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
(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:
You need to type the following:
That’s fairly straightforward, I guess. This one is a little less straightforward:
…and the code you need to type to Flash:
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:
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:
Guess what name you need to plug into the TextFormat.font property…
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?
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:
- Make a dynamic text field in the Flash authoring tool and set its font to the font name you are trying to figure out.
- Give that dynamic text field an instance name (let’s use
myFieldin this example)
- Then type this code into the ActionScript panel:
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.