This post piggy-backs on the previous post about adding text to the screen.? Sometimes just adding text does not cut it.? This matters specifically when you want flash to render a font and not rely on the fonts that the user may or may not have installed.? Doing so will ensure that the text you want to display is displayed the way you want and the same way for everyone.? The font file is actually embedded in your SWF file.
I came across font embedding for a different reason.? I wanted to put dynamic text on the screen and then modify some of its properties.? I tried to change the alpha of the text and quickly realized that it did not work.
When you embed the font with the SWF flash actually renders the text which allows you to apply filters to the text and change properties such as the alpha.? This is not possible with out embedding.
Embedding is easy!
Start first in your Flash IDE and right click inside the library.? Then click "New Font..." select the font you want, name it whatever you want, and click ok.? Note:? If you are using a font and you also want to use a bold face of the font you need to import them separately.
Now you should see the font listed in your library.
Now for the fun part:? right click the font in your library and click "linkage."? When the linkage window opens check "export for actionscript" and pick a class name that you will remember.? For Futura Medium I chose "FuturaMed."
Now you can access the font with actionscript!
Lets go in to the actions panel and write a little code.
First we need to create a font object:
var myFont:Font = new FuturaMed();
This creates a new instance of the font we embedded in the Flash library.? Notice that the font object name "FuturaMed" should be the same as the Class name you gave your font on the linkage panel.
There is really not much else left to do.? For the sake of saving space I wiill refer to my previous post Let’s make a text field appear.? We just need to change a few more things to apply the font object to the text.? First we need to attach the font to our textFormat object.? In the last example I used and object called "textFormat."
textFormat.font = myFont.fontName;
Now the font object is attached to the textFormat object that is applied to your textField object.
You would probably assume, like I did, that it would just work now.? Nope.? You need to add one more line of code to your textField object.
myText.embedFonts = true;
Now you are all set.? Below I will include the complete code for this exercise so you can see it all together.
-
// Create your font instance
-
var myFont:Font = new FuturaMed();<strong>? </strong>
-
-
// Set your text formating up
-
var textFormat:TextFormat = new TextFormat();
-
textFormat.size = 20;
-
textFormat.color = 0xFF0000;
-
-
textFormat.font = myFont.fontName;? //? here we apply the font to the formatting object
-
-
//? Set your text field up
-
var myText:TextField = new TextField();
-
myText.autoSize = TextFieldAutoSize.LEFT;
-
theText = "Hello world.";
-
myText.text = theText;
-
-
myText.embedFonts = true; ? ? ? ? // set embedFonts to true and we are good to go.
-
-
myText.setTextFormat(textFormat);
-
-
// put it on the stage
-
addChild(myText);




6 comments ↓
Thank you.
Thank you so much… This is so simple!
You saved my ass…
Thanks very much. This is not easy info to find so plainly stated.
I spend like 30 minutes trying to figure out why it wasn’t working. It turns out some fonts are not embeddable XD.
How to refer to the embedded font in an external class?
externalClass.as
=============
var myFont:Font = new FuturaMed();
1180: Call to a possibly undefined method FuturaMed
СпаÑибо. было очень интереÑно.
Leave a Comment