I’ve been googling for half a day and pulling a lot of hair out until I found this post.
I thought I’d summarize this great bit of wisdom with some key words for anyone else searching out there.
Specifically, in my case I had issues with using multiple fonts with CSS in an HTML textfield.
**The Setup
**
You’re using AS3/CS3/Flex/mxmlc. You’ve got some texfields with HTML code and a little CSS for good clean markup. You’d like to embed some fonts to make sure things appear consistently on any platform.
The Problem
After learning about the “Embed” meta tag you’ve managed to embed a font and display it. But variations of fonts (bold, italic) come in different files and you can’t get them to work at the same time in a single text field.
After some thinking you decided to embed several fonts under the same family name, like so:
[Embed(source="../fonts/Verdana.ttf", fontWeight="normal", fontName="VerdanaRegular", fontFamily="Verdana", mimeType="application/x-font-truetype")]
public static const VerdanaRegularTTF:String;
[Embed(source="../fonts/Verdana_Bold.ttf", fontWeight="bold", fontName="VerdanaBold", fontFamily="Verdana", mimeType="application/x-font-truetype")]
public static const VerdanaBoldTTF:String;
Then you try to pass the fontFamily to CSS:
...
var defaultStyle:Object = new Object();
defaultStyle.fontFamily="Verdana";
But this doesn’t work. CSS doesn’t recognize the fontFamily from the Embed tag; it only recognizes the fontName tag.
You also try to set a text format for your textfield (thinking of passing the fontFamily name with format.font=“Verdana”). But it seems that ‘format’ isn’t allowed with CSS.
The Solution
Use a single fontName for the fonts when embedding them. Pass this single fontName to CSS (you don’t need to include a fontFamily field). Also make sure to include the proper fontStyle/fontWeight fields when embedding italic/bold fonts. Finally, remember to set the “embedFonts” property to ‘true’ for your textfield.
Here are the important snippets. Embedding:
[Embed(source="../fonts/Verdana.ttf", fontName="Verdana", mimeType="application/x-font-truetype")]
public static const VerdanaTTF:String;
[Embed(source="../fonts/Verdana_Bold.ttf", fontWeight="bold", fontName="Verdana", mimeType="application/x-font-truetype")]
public static const VerdanaBoldTTF:String;
[Embed(source="../fonts/Verdana_Italic.ttf", fontStyle="italic", fontName="Verdana", mimeType="application/x-font-truetype")]
public static const VerdanaItalicTTF:String;
[Embed(source="../fonts/Verdana_Bold_Italic.ttf", fontWeight="bold", fontStyle="italic", fontName="Verdana", mimeType="application/x-font-truetype")]
public static const VerdanaBoldItalicTTF:String;
Using CSS with the textfield:
var defaultStyle:Object = new Object();
defaultStyle.fontFamily = "Verdana";
var style:StyleSheet = new StyleSheet();
style.setStyle(".defaultStyle", defaultStyle);
var t:TextField = new TextField();
t.embedFonts = true;
t.styleSheet = style;
t.htmlText = "<span class='defaultStyle'>My <b>bold</b> text</span>";
And you’re done.