Loading External Fonts in Actionscript 3.0

First step:

– create a flash file ( font.fla) linked to a actionscript class file (Font.as).
– copy and paste the code bellow in Font.as

package
{
import flash.display.MovieClip;

public class CustomFont extends MovieClip
{
[Embed(source='/font.ttf', fontName='CUSTOM', mimeType='application/x-font')]
public static var CUSTOM:Class;
}
}

– compile the files, now you should have a font.swf in your folder.

Second step:

– create a flash file ( main.fla) linked to a actionscript class file (Main.as).
– copy and paste the code bellow in Main.as

package
{
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.text.Font;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;

public class Tutorial extends MovieClip
{
public function Tutorial()
{
var $loader:Loader = new Loader();
$loader.contentLoaderInfo.addEventListener(Event.COMPLETE, Complete, false, 0, true);
$loader.load(new URLRequest("font.swf"));
}

private function Complete($e:Event):void
{
var FontLibrary:Class = $e.target.applicationDomain.getDefinition(getQualifiedClassName($e.target.content)) as Class;
Font.registerFont(FontLibrary.CUSTOM);

ShowText();
}

private function ShowText():void
{
var $tf:TextField = new TextField();
$tf.defaultTextFormat = new TextFormat("CUSTOM", 60,0x000000);
$tf.width = 500;
$tf.embedFonts = true;
$tf.text = "HELLO WORLD";
addChild($tf);
}
}
}

– compile the files and you should have your text with your custom font.

Advertisements

3 thoughts on “Loading External Fonts in Actionscript 3.0

  1. Pingback: Loading External Fonts in Actionscript 3.0 « code.zombie | Flash Designers

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s