Embedding fonts in textfields dynamically AS3

Embedding fonts dynamically into generated text fields might look complicated but it’s not too bad once you get the hang of it. The below code creates a ‘defaultFormat’ which will be applied to all generated textFields – (setTextFormat(format) can be called to change it if required).

// default textformat
var textFormat:TextFormat = new TextFormat();
var defaultFont:Font = new Helvetica_Neue();
textFormat.font = defaultFont.fontName;
textFormat.size = 14;
textFormat.color = 0x666666;

// textField
var txtField:TextField = new TextField();
txtField.autoSize = TextFieldAutoSize.LEFT;
txtField.defaultTextFormat=textFormat;
txtField.embedFonts = true;
txtField.text = "Test Font";
txtField.selectable = false;

addChild(txtField);

The only bit not here is to embed the font within the flash library. You need to create the font as a class, in my example above it is exported for actionscript as Helvetica_Neue. (line3)

  1. Right-click in the library (Flash CS4 +?)
  2. Select new Font…
  3. Check export for actionscript, then give it whatever name you want in the class field. Use that reference to import your various fonts.

Note: with defaultTextFormat, you need to call that before embFonts else it’ll embed no fonts and you won’t see your text. So you set your text format first, before calling embed fonts.

This article is better than this
http://sara-intop.blogspot.com/2007/10/embedding-font-in-flash-cs3using-as3.html

Tech Reference:

Leave a Reply

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

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑