Embedding Fonts Using ActionScript 2.0
Posted by Anthony in Flash, Tutorials tagged actionscript 2, Flash, fonts, text format
I work on a number of Flash websites that require specific fonts to be used, most of which aren’t part of the list of common fonts normally found on computers. While this isn’t a huge deal for static text, it does pose a problem for dynamic and input text fields, especially when you make text fields on the fly using ActionScript. Below you’ll find how to embed fonts into your Flash movie using ActionScript 2.0.
To start, you’ll need to make a new Font item in your Library. Click on the Library menu, and select “New Font…”. Find your Font and Style from the dropdown menus in the dialog box, and give it a unique name. I generally give it a name that includes reference to both the Font and Style (in this example, I am using RockwellBold). Then, make sure the “Export for ActionScript” checkbox is checked under the Linkage section of the dialog box, which will auto-check the “Export in frame 1″ checkbox. Lastly, give your new Font a unique identifier in the “Identifier” field beneath the checkboxes. I like to make this Identifier the same as the Name I gave it above. Once all that is done, click OK.
Next comes the ActionScript:
var rockwellBoldTF:TextFormat = new TextFormat();
rockwellBoldTF.font = "RockwellBold";
myText_txt.text = "Hello World!";
myText_txt.setTextFormat(rockwellBoldTF);
myText_txt.embedFonts = true;
First, we create a new TextFormat, and give it the font attribute with the name of the Font in your Library that you just created. Then, set the text of your text field (“Hello World!”). After setting the text, use the setTextFormat attribute, and pass in the TextFormat variable you created in the first line. Lastly, set the embedFonts attribute to “true“, and you’re all set.
As you no doubt noticed, it’s nothing too complicated to embed fonts. I’d also urge you to explore more of the options of text formatting, colors, etc. And while this example uses ActionScript 2.0, make sure you keep an eye out for a post on how to make it happen in ActionScript 3.0. Until next time, happy coding!


Facebook
LinkedIn
Twitter
There are no comments yet, add one below.