flash's font hell 3 - Accessing the embedded font
Today we will fill a textfield and let it use an embedded font.
To fill a textfield it provides us two properties text and htmlText. It doesn't matter how the text field was created(in the WYSIWYG editor of programatically.)
Text with directly embedded font
This is the easiest way to asign a text. But it is less flexible too. We can't format the text at runtime. We just can change the textfield's properties in Flash and export the film again.
- textField.text = "Today is a wonderful day!";
Text property with font from the library
This is basically working in the same way as the example above. But to asign a Font we need some additional lines of code.
- textField.text = "Today is a wonderful day!";
- var newFormat:TextFormat = new TextFormat();
- newFormat.color = 0xFF0000;
- newFormat.size = 18;
- newFormat.underline = true;
- newFormat.italic = true;
- newFormat.font = "Arial";
- textField.setTextFormat( newFormat );
HtmlText with directly embedded font
This is simply not possible to do.
HtmlText with font from the library
This is the most flexible way and it needs surprisingly less lines of code. There are just some points to care about when using this methode:
- All formatting issues has to be done in html.
- The embedFont property of the text field has to be set to true.
- textField.embedFonts = true; // if we forget this line the text field wont show anything.
- textField.htmlText = "<font face='Arial' size='12' >Today <i>is a </i> <b>wonderful</b> <font color='red'>day!</font></font>";
So I think that's it. This was a small series with the results of the five day studies about the text field. I hope it saves you the time I invested in finding this out. And hopefully you will find it that usefull that you link to it :-)
Oh wait! There is just one more thing!
Font name
When we embed a font into the library we have to activate the checkbox "export for actionscript". This says flash to put the font into the swf file. With activating this option we have to give this library item an export name too. Unfortunately this isn't the name of the font we have to use in actionscript. While compiling the swf file flash generates new names for the embedded fonts. So at authoring time of the swf file we don't know the name flash will generate for a font.
How do we know the names of the embedded fonts? We have to write a small script that traces all font names. After executing it one time we can go back to our source code and access our font with its name.
- var embeddedFonts:Array = Font.enumerateFonts(false);
- for ( var i:int = 0; i < embeddedFonts.length; i++)
- trace( "'"+embeddedFonts[i].fontName+"'", embeddedFonts[i].fontStyle, embeddedFonts[i].fontType );
In most cases the ordinary font name and the name flash generates to access it via actionscript will be the same. As soon as we embed a font as bitmap font its guaranteed a generic name. So keep it in mind.
Well, that's it.
Here you can download a zip file containing three flas to see all the stuff working.
- 0 Comments



Your comment