I’m trying to load a custom font to use at runtime in AS3. The code seems to work but the font is not displayed properly. Could you point what I’m missing?
[LIST=1]
[]The SWF with the font item inside Library, which is exported with “GoodTimes” name, using “flash.text.Font” as the superclass.
[]FLA and its respective AS document class file. The code is quite simple, look:
[/LIST]
package{
import flash.display.Sprite;
public class Test1 extends Sprite
{
import flash.display.Loader;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
import flash.events.Event;
import flash.utils.getDefinitionByName;
import flash.text.Font;
import flash.text.TextField;
import flash.text.TextFormat;
static var GoodTimes:Class;
function Test1():void
{
var loader:Loader = new Loader();
var url:URLRequest = new URLRequest("temp/GoodTimes.swf");
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loader.contentLoaderInfo.addEventListener(Event.INIT, this.good);
loader.load(url, context);
}
function good(e:Event):void
{
Test1.GoodTimes = getDefinitionByName("GoodTimes") as Class;
trace(">:", Test1.GoodTimes);
Font.registerFont(Test1.GoodTimes);
this.startText();
}
function startText():void
{
trace("start drawing the TextField");
var tf:TextField = new TextField();
tf.width = 500; tf.height = 250;
tf.x = tf.y = 50;
tf.border = true;
tf.defaultTextFormat = new TextFormat("GoodTimes", 12);
trace(tf.defaultTextFormat.font);
tf.text = "Sample text that should use GoodTimes, but shows itself in Default system font. What could be missing?";
addChild(tf);
}
}
}