I’ve been building Flash sites and applications for a while now and I still sometimes forget how to properly embed fonts for use with dynamic text. I’m not talking about populating existing TextFields that have manually had fonts embedded in them, but rather more complex situations where you are either dynamically creating TextFields or you want to have complex formatted paragraphs, perhaps containing different fonts and weights in the one field. This is when things start to get a bit trickier.

 

Benefits of Embedding Fonts

  • You can be sure the font will display (even if the user does not have the font installed on their computer)
  • You have the option to use antialiasing
  • Centrally change fonts used in all text fields at once

If you are using a CMS for Flash such as Rainbow Live, you will want the text to display in your swf file the same way it looks in the rich text editor, and that means embedding fonts. What can make this confusing is that if the font is installed on your computer (as it probably will be seeming as your the developer) the text will appear to display properly while you test on your machine, however when someone views the file that doesn’t have the font installed, they will be greeted with a default font instead, and that could really kill your site’s mojo (Times New Roman anyone?). One clue you might have as to an unembedded font is that it will not display antialiased.

Comparison between embedded and non embedded font in Flash
 

Two Approaches

So we’re going to go through two approaches here and the difference depends on how you want to work, as well as if you want font and style variations in your text display. These examples will be as code focused as possible, but there is no reason why you can’t apply the same approach to TextFields on the stage or from the library.

 

Method 1 – Embedding for use with a single font (for use with the TextField.text property)

I use this approach when I only need to have a single font per TextField, but I want the flexibility to be able to change what that font is via code without having to go in and change the font properties on an actual TextField in the Flash IDE.

The first thing to do is embed some fonts. For this example we’ll choose two fonts, I’ve chosen Comic Sans and Impact as they are both easily recognisable and it will be easy to double check if they are displaying properly. To embed the fonts, go to the Flash IDE and do the following:

 

Embedding a Font in the Library

  1. Open up the library and select “New Font” from the properties menu on the top right
    Opening the library properties window
  2. Choose a name for your font. I like to take a styles approach and name the fonts acccording to their function as opposed to font name, so for the name value I’ll put “heading_font”.
    Font Properties Window
  3. Choose which font and font style you want, in this case I’ll choose Impact and as there are no styles (Usual options ar Normal, Bold, Italic and Bold Italic) I won’t bother selecting a style. Remember Bold, Italic etc. are considered seperate fonts as far as Flash is concerned and have to be embedded individually.
  4. Tick the “Export for Actionscript” option in the linkage section and add a Class name for the font, I’ll keep this as “heading_font” – this will be the id we use to refer to the font later on. We’ll leave Export in Frame 1 ticked so that we can simply call the font from code
  5. That’s it, press ok and close the Font Symbol Properties window, you’ll notice that there is now a font in your library named “heading_font”
    Embedded font in library

 

Now that the font is embedded let’s see how we use that font in a TextField. Remember you can do this with a TextField you’ve manually created and placed on the stage, but I will be creating the field with code in this example.

 

 

As you can see in the code above we simply create an instance of the font, and then use the fontName property to set this as the font of a new TextFormat. We can then use this TextFormat to style and TextField we choose. You could reference the font name directly without creating a Font instance such as:

var myFormat:TextFormat = new TextFormat("Impact",20)

But I find that there are a couple of downfalls with this approach. Firstly it means that you are being specific about which font you are using for the headline, this means that if you change your font from Impact to Helvetica you are going to have to come in and change your code so that you’re telling the TextFormat to use “Helvetica” instead of “Impact”, abstacting this part means that you only have to update the font in the IDE and everythign will continue to work. The second advantage is if you are working with swcs as opposed to an FLA file (You might be working with FlashDevelop, Flash Builder or a similar development environment) your font won’t actually be embedded unless it is mentioned explicitly somewhere in the code, hence this technique kills two birds with one stone.

Similarly, to embed a second font for your body copy follow the same process by first embedding the font and then aplying it with code. You might end up with something along the lines of the following:

 

 

And heres our output:

Embedding fonts in flash method one

 

Method 2 – Embedding for use with complex multi-font formatting (for use with the TextField.htmlText property)

The second technique is for those times when you wish to use more than one font, or even just use more than one font style (such as using Bold and Italic) as these are considered separate fonts by Flash.

You start things off the same way by embedding the fonts that you’ll need to use, remember to embed Bold, Italic and Bold Italic as individual fonts. Follow the ‘Embedding a Font in the Library’ section above.

embed_font_styles

In this example I’ll use the same two fonts, Impact and Comic sans, but instead of using two separate TextFields to display the Heading and Body I will display them both in the one field. This is useful for a number of reasons, but in general it means that you can display a chunk of HTML formatted text in the one TextField without having to create a new one for each piece of different formatting within the text block. This is great for working with Rich Text Editors in a Flash CMS such as the one in Rainbow Live, which means the user can edit and format the text as they wish and it can display within Flash without having to be limited by a predefined content structure.

 

Here’s a screen shot of the text as edited in Rainbow Live:

The Rich Text Editor in Rainbow Live Flash CMS

 

You can see there are two heading sections and a couple of paragraphs along with some bold and italic text. You can display embedded fonts at any size so there is a smaller footnote section thrown in for good measure also.

The trick behind Rich Text Editors in Flash is that they use a subset of Flash Friendly HTML to style the text. Flash only supports some of the more simple HTML elements such as <p>, <a> and <img>,for a complete list view the support docs from adobe.

 

Here’s the HTML output from Rainbow:

<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Impact" SIZE="20" COLOR="#333333" LETTERSPACING="0"
    KERNING="0">Here Is An Example Headline in Impact</FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Comic Sans MS" SIZE="12" COLOR="#333333"
    LETTERSPACING="0" KERNING="0"></FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Comic Sans MS" SIZE="16" COLOR="#818181"
    LETTERSPACING="0" KERNING="0">Here is some body copy for the
    text embed example. This data has been formated with the
    <I>Rich Text Editor</I>in
    <B>Rainbow Live*</B></FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Comic Sans MS" SIZE="12" COLOR="#818181"
    LETTERSPACING="0" KERNING="0">
      <I>* learn more about</I>
      <FONT COLOR="#333333">
        <I></I>
        <FONT COLOR="#3452FF">
          <A HREF="https://www.codeandvisual.com/rainbow"
          TARGET="_blank">
            <I>Rainbow Live Flash CMS</I>
          </A>
          <FONT COLOR="#818181"></FONT>
        </FONT>
      </FONT>
    </FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Comic Sans MS" SIZE="12" COLOR="#333333"
    LETTERSPACING="0" KERNING="0"></FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Comic Sans MS" SIZE="12" COLOR="#333333"
    LETTERSPACING="0" KERNING="0"></FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Impact" SIZE="20" COLOR="#333333" LETTERSPACING="0"
    KERNING="0">Here is a Second Heading</FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Comic Sans MS" SIZE="12" COLOR="#333333"
    LETTERSPACING="0" KERNING="0"></FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Comic Sans MS" SIZE="16" COLOR="#818181"
    LETTERSPACING="0" KERNING="0">If we wanted to do this with the
    first text embed example we would have had to have a separate
    TextField for each heading and copy section. This option allows
    us to have as many heading and body sections as we like within
    the one TextField.</FONT>
  </P>
</TEXTFORMAT>

  

You’ll notice a lot of TEXTFORMAT and FONT tags in the markup. These are created especially for use in Flash and are now superceded in standard HTML. Flash will also recognise HTML formatted without these tags but unfortunately won’t recognise div or table tags (along with many others).

Now that you have some HTML to display, we can display this text very similarly to the first example, with one important difference in particular – use of the TextField.htmlText Property rather than TextField.text

 

 

You’ll see at the beginning of the code we’ve made reference to the font classes the we created and embedded in the library, there’s no need to make instances of them as we don’t need to apply the fonts to the text manually because this is all being set in the HTML content. As long as the classes are referenced by name we can be sure they’ll be available to Flash at run time.

 

Here’s the output:

Method 2 - Multiple fonts and styles in the one TextField

 

Immediately the power of this approach becomes clear – you don’t have to limit or second guess the exact structure of the content being displayed in the text field, and by using an external XML source you’ll have incredible flexibility in what you’ll be able to display. You’ll see that you can change colours and font sizes and even embed links. It’s also possible to display images via a standard <img>, tag within your HTML content, however your control options about how the images display are slightly limited.

 

That’s It!

And so that’s the end of the Flash text embed crash course, I hope you found it a useful reference. If you’d like to download the sample files you can download them here:Download example files

Learn more about Rainbow Live and how to use it a a simple Flash CMS, or even to make your Flash sites more Google Friendly.