Code | Games | Geek

Working embedded fonts in AS3

Leave a comment

Embedding a font into AS3 is frustrating and yet another thing I did not find well explained online. You will need to do 3 things:

  1. [Embed] the font file
  2. define the font as a class
  3. register the font

Embed the font from a relative location to your .as file. Since my .as files are in the proj/src folder, I’m using ../art/Intro.otf as the location of my font. You will need to specify embedAsCFF as false, otherwise Flash will give some nasty errors about Flex libraries missing when you export.

Then, in your constructor of your document class, register the font, so that you can use it elsewhere.

After that, you can use the font anywhere in your code. See the code below:

import flash.text.Font;

// font
[Embed(source="../art/Intro.otf", fontName = "Intro", fontFamily="Intro", fontStyle="normal", fontWeight="normal", mimeType="application/x-font", embedAsCFF="false")]

var Intro:Class;

// Constructor
public function myFlash()
    // font setup


// elsewhere
var textf:TextFormat = new TextFormat();
textf.color =           0xFFFFFF;
textf.font =            "Intro";
textf.size =            48;

Enjoy custom fonts!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s