Correct font names in TextFormat.font (AS3)

If you want to use TextFormat in order to set which font family and style you wish to use on a component in your Flash project you just might be a bit puzzled on how to express both family and style in just one field - TextFormat.font.

The problem

If you plan to use the regular version of the font family ("Veto Com" in the sample, but could be "Arial", "Verdana" or the font of your choice), you'll just go with:
import flash.text.TextFormat;

var textFormat:TextFormat = new TextFormat();
textFormat.font = "Veto Com";
But if you want to use the Light style instead of Regular it's a bit trickier, because you need to know the font name including both family and style. Unfortunately there are no standards so it might differ between font sets.

The solution

There are two ways to get the correct font name:

1. In Windows, locate the font file and open it to display the font information window. It will look something like this and the correct font name to use will be displayed in upper left corner:


2. Add a textfield to your project and give it an instance name. Set both the Family and Style properties to the font you want. Then in code access the font property of the textfield and trace it using trace(myTextField.getTextFormat().font);
Source for this solution: http://blog.erikphansen.com/actionscript-textformatfont-values/

Enter the font name that you get from either of the methods above in your code:
import flash.text.TextFormat;

var textFormat:TextFormat = new TextFormat();
textFormat.font = "Veto Com Light";

Related posts:

Comments

comments powered by Disqus