Tuesday, September 25, 2007

Embedding a font in a component

it took me quite a while and a variety of sources to work out how to set an embedded font for use in a comboBox component in AS3, so i thought i would record the solution for others(and myself in future when i forget!).

Here are steps to follow:

Step 1. add the font you wish to embed to the library, set it to 'Export for ActionScript' and give it a Class name.(in this case 'ComicSans')
Step 2. add a comboBox component to your stage.
Step 3. add the following script:

var Comic:TextFormat=new TextFormat();
Comic.font=new ComicSans().fontName;
Comic.size=12; //let's say we want to change the font size

combo.textField.setStyle("embedFonts", true);
combo.textField.setStyle("textFormat", Comic);
combo.dropdown.setRendererStyle("embedFonts", true);
combo.dropdown.setRendererStyle("textFormat", Comic);


A couple of points to be aware of:

when setting your TextFormat font, don't use the font's linkage identifier directly, this property requires the name that the system uses when referring to the font.
rather request the name of the font through the font class.
i did this above with the line:
Comic.font=new ComicSans().fontName;

the font of the textfield within the combo needs to set, and the text within the list component(dropdown property) within the combo also needs to be set.
these are set differently.
while the textfield is set directly with setStyle, the list component is set indirectly, with setRendererStyle as there will be multiple textfields within the list component.

No comments: