Embed Font workflow for flash as3

Martin, a good friend of mine, wrote a great blog post about font handling. He created an ant task which creates a font class / swf. What’s so cool about it? You can load fonts, and dont have to compile them every time, like [Embed ...] does.

I just had some trouble because I misspelled the font :O. Here is a loop which traces all the registered font names and solved my really stupid fault. And here is a example how you embed a registered font.

var fontLoader : Loader = new Loader();
var lInf : LoaderInfo = fontLoader.contentLoaderInfo;
lInf.addEventListener(Event.COMPLETE, onFontLoaded);
 
fontLoader.load(new URLRequest("Verdana.swf"));
 
function onFontLoaded(e : Event) : void 
{
 var tf : TextFormat = new TextFormat();
 tf.font = "FONTNAME";
 
 var tfield : TextField = new TextField();
 tfield.defaultTextFormat = tf;
 tfield.text = "Martin rocks!";
 
 var embeddedFonts : Array = Font.enumerateFonts(false);
 for(var i : Number = 0;i < embeddedFonts.length; i++)
 {
 var item : Font = embeddedFonts[i];
 trace(
 "[" + i + "] name:" + item.fontName + 
 ", style: " + item.fontStyle + 
 ", type: " + item.fontType
 );
 }
}

 

Leave a Reply

Formatting: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">