Tink » Blog Archive » Embedding fonts in AS3

Super dad movies Add comments

Embedding fonts in AS3

No more do you have to have fonts in your library you can embed them directly from the folder where they reside using the [Embed] in your ActionScript Project.

You supply the path to the font in the ’source’ param and then store a name to be referenced to access the font in ‘fontFamily’. You also need to declare a variable of type String or Class for the [Embed] to be assigned to, although you don’t need to reference it in your code.

To access the embeded font you use the String you assigned to the var ‘fontFamily’

Actionscript:
  1. package
  2. {
  3.        
  4.        
  5.         import flash.display.Sprite;
  6.         import flash.display.TextField;
  7.         import flash.display.TextFieldAutoSize;
  8.        
  9.         import flash.text.TextFormat;
  10.         import flash.text.AntiAliasType;
  11.        
  12.        
  13.        
  14.         public class EmbedFontTest extends Sprite
  15.         {
  16.                
  17.                
  18.                
  19.                 [Embed(source="C:\WINDOWS\Fonts\ARIAL.TTF", fontFamily="Arial")]
  20.                 private var _arial_str:String;
  21.                
  22.                 private var _arial_fmt:TextFormat;
  23.                 private var _text_txt:TextField;
  24.                
  25.                
  26.                
  27.                 public function EmbedFontTest()
  28.                 {
  29.                         super();
  30.                        
  31.                         this.initEmbedFontTest();
  32.                 }
  33.                
  34.                
  35.                
  36.                 private function initEmbedFontTest():Void
  37.                 {
  38.                         this._arial_fmt = new TextFormat();
  39.                         this._arial_fmt.font = “Arial”;
  40.                         this._arial_fmt.size = 40;
  41.                                                
  42.                         this._text_txt = new TextField();
  43.                         this._text_txt.embedFonts = true;
  44.                         this._text_txt.autoSize = TextFieldAutoSize.LEFT;
  45.                         this._text_txt.defaultTextFormat = this._arial_fmt;
  46.                         this._text_txt.text = “Test Arial Format”;
  47.                   
  48.                         this.addChild(this._text_txt);
  49.                 }
  50.         }
  51.        
  52.        
  53.        
  54. }

This entry was posted on Monday, December 5th, 2005 at 2:04 am and is filed under Flash Player 9, ActionScript 3.0. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Cool article how to embed a font in a pure AS3 actionscript project using Flex. There are 2 errors in the script. First the namespace of TextField is in flash.text
Second the return type of the initEmbedFontTest should be void instead of Void

Posted via web from wiibart’s posterous



Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in