AS3 Embed My Font Utility

Here’s a quick ActionScript font utility, that some of you flash devs may find useful. It uses the most popular way of embedding a font, by referencing a font symbol in the library. If you are not familiar with the process, the following are quick directions on how to create a font in the library.

(1) Open up your Library.
(2) Select the upper-right corner of the Library panel pop up menu.
(3) Select New Font.
(4) Enter a name for the font in the Name field.
(5) Select a font from the Font menu.
(6) Select your desired font style (Regular, Italic, Bold, or Bold Italic) from the Style drop-down menu.

*Note: The Size setting applies only when you use the Bitmap Text option. You’ll usually set the size of the text in a TextFormat object.

(8) Click the Advanced button
(9) Click Export for ActionScript and enter a class name.

*Note: I usually place my library items in a lib namespace, you don’t have to do that.

(10) Click OK.

The font utility simply creates a TextFormat object and sets its fontFamily property to the name of the embedded font, and applies the TextFormat object to a given TextField.

The following is the example swf:

Get Adobe Flash player

Here’s the document class which displays examples on how to use the font utility.

/*******************************
* Author: Manuel Gonzalez      *
* www   : www.codingcolor.com  *
* email : design@stheory.com   *
********************************/

package{
 
import com.utils.TextFields.EmbedFontUtil;
import com.lib.Jazz;
import com.lib.Juice;
import com.lib.Banco;
import com.lib.HelveticaBlack;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.text.Font;
import fl.controls.Button;
import flash.events.MouseEvent;
 
public class DocApp extends Sprite{
   
private var embedUtil   :EmbedFontUtil;
private var exampleString_1 : String = 'A dynamic TextField displaying the Jazz Font';
private var exampleString_2 : String = 'A dynamic TextField with a TextFormat displaying the Juice Font.';
private var exampleString_3 : String = 'The Banco Font, TextFormat and other crap!';
private var exampleString_4 : String = 'Displaying Helvetica Bold in a text field on stage.';
private var exampleString_5 : String = 'Press the button to change my font';
//
private var changeFontButton:Button;
   
   
public function DocApp(){
 embedUtil = EmbedFontUtil.getInstance();

/******** simple example with a dynamic text field *************/
var testField_1:TextField = new TextField();
testField_1.autoSize = TextFieldAutoSize.LEFTtestField_1.x = 25;
testField_1.y = 25;
var JazzFont:Font = new Jazz();//make sure to import your font
embedUtil.embedFont(testField_1,JazzFont);
testField_1.text = exampleString_1;
addChild(testField_1);

/******** simple example with a text field placed manually on stage *********/
var helveticaBlackFont:Font = new HelveticaBlack();//make sure to import your font
embedUtil.embedFont(textFieldOnStage,helveticaBlackFont);
textFieldOnStage.text = exampleString_4;
/****************************************************************************/

/******** example with a dynamic text field & TextFormat object ************/
var testField_2:TextField = new TextField();
testField_2.autoSize = TextFieldAutoSize.LEFT;
testField_2.x = 25;
testField_2.y = 50;
var format_1:TextFormat = new TextFormat();
format_1.color = 0xff00ff;
format_1.align = TextFormatAlign.LEFT;
format_1.size = 14;
var juiceFont:Font = new Juice();//make sure to import your font
embedUtil.embedFont(testField_2,juiceFont,format_1);
testField_2.text =  exampleString_2;
addChild(testField_2);
     
/************************************************************************/
     
     
/******** example with a dynamic textfield & properties including TextFormat**********/
var testField_3:TextField = new TextField();
testField_3.width = 500;
testField_3.height = 22;
testField_3.background = true;
testField_3.backgroundColor = 0x000000;
testField_3.textColor = 0xffffff;
testField_3.x = 25;
testField_3.y = 85;
     
var format_3:TextFormat = new TextFormat();
format_3.size = 16;
format_3.align = TextFormatAlign.CENTER;
format_3.url = "http://codingcolor.com";
format_3.underline = true;
     
var bancoFont:Font = new Banco();//make sure to import your font
embedUtil.embedFont(testField_3,bancoFont,format_3);
testField_3.text =  exampleString_3;
addChild(testField_3);

/*************** example with a textfield on stage preserve format and string change font ****/
createButtonUI();
changeFontField.text = exampleString_5;

}
/*
Method: createButtonUI
Parameters:
Returns:
*/

private function createButtonUI():void
{
changeFontButton = new Button();
changeFontButton.width = 100;
changeFontButton.move(changeFontField.x,changeFontField.y + changeFontField.height+10);
changeFontButton.label = "Change Font";
changeFontButton.setStyle("textFormat", new TextFormat("Helvetica Black"));
changeFontButton.addEventListener(MouseEvent.CLICK, changeTheFont,false,0,true);
addChild(changeFontButton);
}

/*
Method: changeTheFont
Parameters:
evt:MouseEvent
Returns:
*/

private function changeTheFont(evt:MouseEvent):void
{
var JazzFont:Font = new Jazz();
embedUtil.embedNewFontPreserveString(changeFontField,JazzFont);
}
}
 
 
 
}

As you can see it’s pretty simple to use:
var helveticaBlackFont:Font = new HelveticaBlack();
embedUtil.embedFont(textFieldOnStage,helveticaBlackFont);
textFieldOnStage.text = exampleString_4;

Download the class and files below.
Enjoy!
Font Embed Util (323)

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: