Create a Flash AS3 application that loads an external font at runtime… Sounds easy enough… But it is a little tricky. I needed to create a flash app that allowed users to choose a font from a list and load that into a player. I wanted to offer several fonts but did not want to bloat the load-time with unused fonts. I could not find much help on this topic… Luckily I found
betriebsraum and with that I was able to hack up something that worked for me.
Step 1: Created an external swf with the font I needed. (I created separate ones for each font I was going to offer.)
- Created a new Fla and add a font to the library.
- In the Library pane(F11), click the options dropdown, and choose ‘New Font’
- Select a font, size and style–size is not important (arguably)–however, I believe you will have to create separate fonts for each style you require. The name is not important really either.
- Right click the new font in the Library and select ‘Linkage…’
- Check ‘Export for ActionScript’ and ‘Export in first frame’.
- Give it a Class:–this name is important and you will need to remember it. I used ‘EFont01′ fig. 1

- Publish the swf as ‘/font-Fontname.swf’.
Step 2: Create the Application:
- Create a new Flash CS3/As3 application set the Document class as ‘Main’ and save it as ‘/myapp.fla’
- Create a new AS3 script and save it as ‘/Main.as’
Main.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| package {
import flash.display.Sprite;
import flash.text.*;
import flash.events.*;
public class Main extends Sprite {
var t:TextField = new TextField();
var f = new TextFormat();
public function Main() {
var font = new LoadFont("font-Zapfino.swf",["EFont01"]);
font.addEventListener(LoadFont.COMPLETE, successLoadFont);
font.addEventListener(LoadFont.ERROR, failLoadFont);
}
private function failLoadFont(e:Event){
f.font="Arial"; //default font if load fails.
t.embedFonts = false;
post();
}
private function successLoadFont(e:Event){
var embeddedFonts:Array = Font.enumerateFonts(false);
f.font=embeddedFonts[0].fontName;
t.embedFonts = true;
post();
}
public function post(){
f.size = 36;
t.defaultTextFormat = f;
t.x = 100;
t.y = 100;
t.background = true;
t.autoSize = TextFieldAutoSize.LEFT;
t.text = "We are in loaded Font";
addChild(t);
}
}
}
|
- Create a new AS3 script and save it as ‘/LoadFont.as’
LoadFont.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.errors.*;
import flash.system.*;
import flash.net.*;
public class LoadFont extends EventDispatcher {
public static const COMPLETE:String = "complete";
public static const ERROR:String = "error loading font";
private var loader:Loader = new Loader();
private var _fontsDomain:ApplicationDomain;
private var _fontName:Array;
public function LoadFont(url:String,fontName:Array):void {
_fontName = fontName;
trace("loading");
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, font_ioErrorHandler);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,finished);
loadAsset(url);
}
private function loadAsset(url:String):void {
var request:URLRequest = new URLRequest(url);
loader.load(request);
}
function finished(evt:Event):void {
_fontsDomain = loader.contentLoaderInfo.applicationDomain;
registerFonts(_fontName);
dispatchEvent(new Event(LoadFont.COMPLETE));
}
function font_ioErrorHandler(evt:Event):void {
dispatchEvent(new Event(LoadFont.ERROR));
}
public function registerFonts(fontList:Array):void {
for (var i:uint = 0; i < fontList.length; i++) {
Font.registerFont(getFontClass(fontList[i]));
}
}
public function getFontClass(id:String):Class {
return _fontsDomain.getDefinition(id) as Class;
}
public function getFont(id:String):Font {
var fontClass:Class = getFontClass(id);
return new fontClass as Font;
}
}
}
|
This entry was posted on Tuesday, May 13th, 2008 at 11:23 am and is filed under Flash.
May 13th, 2008 at 4:38 pm
One of the best new additions for dealing with dynamic fonts is the ability to search for them dynamically in memory space. Here is some code that would complement the example shown here; it retrieves a font dynamically at runtime, allowing you to get a list of available fonts and then use one of them for a text field:
May 13th, 2008 at 4:39 pm
May 14th, 2008 at 12:10 pm
July 16th, 2008 at 1:53 am
Are loaded fonts able to be used by SWF’s on other pages other than the webpage the font was loaded on.
July 22nd, 2008 at 11:13 am
September 5th, 2008 at 8:10 am
September 29th, 2008 at 1:55 pm
September 29th, 2008 at 11:16 pm
November 10th, 2008 at 9:22 am
Thanks a lot men. U did a great work.
December 1st, 2008 at 10:07 pm
December 2nd, 2008 at 12:30 pm
December 2nd, 2008 at 4:08 pm
December 10th, 2008 at 3:38 am
December 10th, 2008 at 3:39 am
January 5th, 2009 at 3:53 pm
January 30th, 2009 at 6:31 pm
February 4th, 2009 at 6:18 pm
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, font_ioErrorHandler); // adding contentLoaderInfo
for (var i:uint = 0; i < fontList.length; i++) { // the < simbol
May 26th, 2009 at 7:09 am
{
for (var i:Number = 0; i < 4 ; i++)
{
coantainerfonts.name = “fontconatiner”+i
coantainerfonts.x = 0
coantainerfonts.y=i*(18)
var tempname:String = “EFont0″+(i+1)
var font = new LoadFont(tempStr, [tempname]);
font.addEventListener(LoadFont.ERROR, failLoadFont);
private function successLoadFont(e:Event)
{
var TxtRef:TextField = mc.txt as TextField
TxtRef.text =embeddedFonts[_counter].fontName
August 14th, 2009 at 9:07 am
November 19th, 2009 at 8:13 am
December 8th, 2009 at 9:49 am
December 9th, 2009 at 2:08 am
April 13th, 2010 at 1:32 am
i have use your font loading classes. this is good but i m facing problem with Arial font……
i have check other system fonts, all r working perfectly with styling but when i use Arial this code is break. this is not working, text is not visible aftre using Arial font with this class
May 18th, 2010 at 11:05 am
August 3rd, 2010 at 12:45 pm
June 11th, 2011 at 4:45 am
June 11th, 2011 at 12:58 pm