back to krasimirtsonev.com
to blog's home page

AS3: runtime font loading (embedding)

Flash is wonderful technology and has many advantages. Before a couple of years it was really popuplar to use it for displaying fonts that are not installed on the user's machine. Flash is still powerfull tool for such kind of tasks. These days I worked on a project which had to use a lot of fonts. It wasn't possible to embed all of them so I decided to implement runtime embedding.

Flash still can't load directly a font file. That's why I embeded the font in a swf that had to be loaded later. In my case it was better to create different swf file for every font, but if you prefer you can put all the fonts in only one file. So create a new flash document and open the library panel (press F11). Click on the little button in the top right corner.

AS3: runtime font loading (embedding)

and choose New Font....

AS3: runtime font loading (embedding)

Fill the Character ranges part, choose your font and remember the value of the Family field. After that move to the second tab ActionScript.

AS3: runtime font loading (embedding)

Check Export for ActionScript and Export in frame 1. Type a class name of the font (you will need this name later). Everything is ready and you can compile the swf that will be used.

The following code loads the produced swf file and adds a text field, which uses the font.
There are three major constants in the code:
The first one FONT_NAME is used in the TextFormat definition. You should use the real family name of the font. The second one is the name of the class that you have to fill. And the last one is just the path to the swf file.

You can download the full example's code here.

Sharing ...
Commenting ...
blog comments powered by Disqus