Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flash Developer Center /

Embedding fonts

by Peter deHaan

Peter deHaan

Content

  • Creating a new Font symbol
  • Setting Font symbol properties
  • Setting linkage properties for a Font symbol
  • Creating a new Font symbol instance using ActionScript
  • Setting advanced anti-aliasing
  • Fading text using embedded fonts and the Tween class

Created

1 October 2008

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScript Flash Professional CS4 text

Requirements

Prerequisite knowledge

General experience with programming in ActionScript 3 is suggested.

User level

Beginning

Required products

  • Flash Professional CS4 (Download trial)

Sample files

  • section4.example1.zip (5 KB)
  • section5.example1.zip (5 KB)
  • section5.example2.zip (448 KB)
  • section6.example1.zip (5 KB)

When you specify a font for a TextField in your application, Adobe Flash Player looks for a device font (a font that resides on the user's computer) with the same name. If it doesn't find that font on the user's system, or if the user has a slightly different version of a font with that name, the text display could look very different from what you intended.

To make sure the user sees exactly the right font, you can embed that font in your application's SWF file. Embedded fonts have a number of benefits:

  • Embedded font characters are anti-aliased, making their edges appear smoother, especially for larger text.
  • You can rotate text that uses embedded fonts.
  • Embedded font text can be made transparent or semitransparent.
  • You can use the kerning CSS style with embedded fonts.
  • You can embed almost any font you have installed on your system, including TrueType fonts and Type 1 PostScript fonts.

The main limitation to using embedded fonts is that embedded fonts increase the file size or download size of your application.

There are many ways to embed fonts in a Flash application, including the following:

  • Setting the font and style properties of a dynamic or input TextField on the Stage and clicking the Embed button.
  • Creating and referencing a font symbol, which we'll discuss in this article.
  • Creating and using a run-time shared library containing embedded font symbols, an advanced workflow we won't discuss here.

The next sections describe the most popular and useful ways to use this feature, including creating a new Font symbol, setting Font symbol properties, setting linkage properties for a Font symbol, creating a new Font symbol instance using ActionScript, setting advanced anti-aliasing, and fading text using embedded fonts and the Tween class.

Creating a new Font symbol

To create a new font symbol, select New Font from the library's pop-up menu (in the upper-right corner of the Library panel). This opens the Font Symbol Properties dialog box, which allows you to specify the font symbol's properties.

Creating a new font symbol in a Flash document's library.
Figure 1. Creating a new font symbol in a Flash document's library.

Setting Font symbol properties

The Font Symbols Properties dialog box allows you to define a name for a font symbol, select a font face, and set various styles. The font name is the name that the symbol will have in the Library panel. In order to access this font dynamically using ActionScript, you'll need to set the font's linkage properties (see Setting linkage properties for a new font symbol). The font pop-up menu lets you select the desired font face from a list of fonts installed on the current computer.

Creating a font library item:

  1. Open the library in which you want to add a font symbol.
  2. Select New Font from the Library Panel menu.
  3. Enter a name for the font in the Name field.
  4. Select a font from the Font menu or enter the name of a font in the Font field.
  5. (Optional) Select your desired font style (Regular, Italic, Bold, or Bold Italic) from the Style drop-down menu.
  6. (Optional) To embed the font information as bitmap data rather than as vector outline data, select the Bitmap Text option and enter a font size in the Size text field. (Bitmap fonts cannot use anti-aliasing. If you select Bitmap Text, you must also select Bitmap Text as the anti-aliasing option in the Property inspector for text that uses this font.)

Note: The Size setting applies only when you use the Bitmap Text option. You'll typically set the size of the text in the TextFormat object, which you'll see later.

Specifying a font symbol's properties.
Specifying a font symbol's properties.
Figure 2. Specifying a font symbol's properties (Basic mode, top; Advanced mode, bottom).

Note: You can edit the font symbol's properties any time by right clicking the symbol in the library and selecting Properties from the context menu.

Setting linkage properties for a Font symbol

Before you can instantiate a new Font instance dynamically, you need to make sure it has a linkage class name defined. To set a linkage class:

  1. Right-click the font symbol in the library and select Properties.
  2. Click Export for ActionScript and enter a class name (see Figure 3). If you cannot see the Linkage information in the Font Symbol Properties dialog box, click the Advanced button (see Figure 2, bottom).
  3. Click OK.
Setting a linkage class in the Font Symbol Properties dialog box.
Figure 3. Setting a linkage class in the Font Symbol Properties dialog box.

Note: When dismissing the Font Symbol Properties dialog box, you may get an ActionScript class warning saying that a definition for the specified class could not be found in the classpath, so one will be automatically generated for you. Click OK.

Creating a new Font symbol instance using ActionScript

Once you have a font symbol in your library and a linkage class defined, you can create a new instance of that font symbol using the new operator and specify the linkage class name defined earlier. To apply the embedded font to a dynamically created TextField, you do the following:

  • Create a TextFormat object, set its fontFamily property to the name of the embedded font, and apply the TextFormat object to the TextField. When specifying an embedded font, the fontFamily property should only contain a single name; it cannot use a comma-delimited list of multiple font names.
  • If using CSS styles to set fonts for TextFields, set the font-family CSS property to the name of the embedded font. The font-family property must contain a single name and not a list of names if you want to specify an embedded font.
  • Set the embedFonts property of the TextField to true.

The following example creates a new font instance based on the font symbol in the library with a linkage class name of Font1. Next, the example creates a TextFormat object and sets the font name to the font specified in the myFont instance.

Example

The following example creates a new Font, TextFormat, and TextField instance and sets the embedFonts property to true.

Note: The example requires that a font symbol with the linkage class name of Font1 exists in the library.

// Create a new instance of the Font1 symbol from the document's library. var myFont:Font = new Font1(); /* Create a new TextFormat object, and set the font property to the myFont object's fontName property. */ var myFormat:TextFormat = new TextFormat(); myFormat.font = myFont.fontName; myFormat.size = 24; /* Create a new TextField object, assign the text format using the defaultTextFormat property, and set the embedFonts property to true. */ var myTextField:TextField = new TextField(); myTextField.autoSize = TextFieldAutoSize.LEFT; myTextField.defaultTextFormat = myFormat; myTextField.embedFonts = true; myTextField.text = "The quick brown fox jumped over the lazy dog."; addChild(myTextField);

Result

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

To get the source files for this example, download embeddingfonts_section4_example1.zip from the top of this page.

Setting advanced anti-aliasing

When working with embedded fonts, you can set the anti-aliasing type to either normal or advanced by setting the text field's antiAliasType property to a value in the AntiAliasType class (flash.text.AntiAliasType).

Example

The following example creates a new instance of a Font symbol, enables embedded fonts for a text field and sets the anti-alias type to advanced.

Note: The previous example requires that a font symbol with the linkage class name of Font1 exists in the library.

// Create a new instance of the Font1 symbol from the document's library. var myFont:Font = new Font1(); /* Create a new TextFormat object, and set the font property to the myFont object's fontName property. */ var myFormat:TextFormat = new TextFormat(); myFormat.font = myFont.fontName; myFormat.size = 24; /* Create a new TextField object, assign the text format using the defaultTextFormat property, set the embedFonts property to true, and set the antiAliasType property to "advanced". */ var myTextField:TextField = new TextField(); myTextField.autoSize = TextFieldAutoSize.LEFT; myTextField.defaultTextFormat = myFormat; myTextField.embedFonts = true; myTextField.antiAliasType = AntiAliasType.ADVANCED; myTextField.text = "The quick brown fox jumped over the lazy dog."; addChild(myTextField);

Result

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

To get the source files for this example, download embeddingfonts_section5_example1.zip from the top of this page.

Example

The following example creates a new text field and lets the user toggle whether the font is embedded, which type of anti-aliasing to use (normal or advanced), and the size of the text in the text field.

Note: The previous example requires that a font symbol with the linkage class name of Font1 exists in the library, as well as a CheckBox component, ComboBox component, and a Slider component. An easy way to add the components to the library is to drag them to the Stage, then delete them.

// Import the required component classes. import fl.controls.CheckBox; import fl.controls.ComboBox; import fl.controls.Slider; import fl.events.SliderEvent; // Create a new instance of the Font1 symbol from the document's library. var myFont:Font = new Font1(); /* Create a new TextFormat object, and set the font property to the myFont object's fontName property. */ var myFormat:TextFormat = new TextFormat(); myFormat.font = myFont.fontName; myFormat.size = 24; /* Create a new TextField object, assign the text format using the defaultTextFormat property, set the embedFonts property to true, and set the antiAliasType property to "normal". */ var myTextField:TextField = new TextField(); myTextField.autoSize = TextFieldAutoSize.LEFT; myTextField.defaultTextFormat = myFormat; myTextField.embedFonts = true; myTextField.antiAliasType = AntiAliasType.NORMAL; myTextField.text = "The quick brown fox jumped over the lazy dog."; addChild(myTextField); /* Create a CheckBox component instance which will be used to toggle the embedFonts property. */ var embedFontsCheckBox:CheckBox = new CheckBox(); embedFontsCheckBox.label = "embedFonts"; embedFontsCheckBox.move(0, 50); embedFontsCheckBox.selected = myTextField.embedFonts; embedFontsCheckBox.addEventListener(Event.CHANGE, checkBoxChangeHandler); addChild(embedFontsCheckBox); /* Create a ComboBox component instance which will be used to toggle between normal and advanced anti-aliasing. */ var antiAliasTypeComboBox:ComboBox = new ComboBox(); antiAliasTypeComboBox.addItem({data:AntiAliasType.NORMAL, label:"AntiAliasType.NORMAL"}); antiAliasTypeComboBox.addItem({data:AntiAliasType.ADVANCED, label:"AntiAliasType.ADVANCED"}); antiAliasTypeComboBox.enabled = myTextField.embedFonts; antiAliasTypeComboBox.width = 200; antiAliasTypeComboBox.move(150, 50); antiAliasTypeComboBox.addEventListener(Event.CHANGE, comboBoxChangeHandler); addChild(antiAliasTypeComboBox); // Create a Slider component instance which will be used to set the font size. var fontSizeSlider:Slider = new Slider(); fontSizeSlider.minimum = 8; fontSizeSlider.maximum = 24; fontSizeSlider.value = Number(myFormat.size); fontSizeSlider.tickInterval = 2; fontSizeSlider.liveDragging = true; fontSizeSlider.move(400, 56); fontSizeSlider.addEventListener(SliderEvent.CHANGE, sliderChangeHandler); addChild(fontSizeSlider); /* Handler function for the CheckBox component instance. This function toggles the ComboBox instance's enabled property based on the current value of the CheckBox. */ function checkBoxChangeHandler(event:Event):void { myTextField.embedFonts = CheckBox(event.currentTarget).selected; antiAliasTypeComboBox.enabled = CheckBox(event.currentTarget).selected; } /* Handler function for the ComboBox component instance. This function sets the text field's antiAliasType property based on the value of the currently selected item in the ComboBox. */ function comboBoxChangeHandler(event:Event):void { myTextField.antiAliasType = ComboBox(event.currentTarget).selectedItem.data; } /* Handler function for the Slider component instance. This function sets the text format's font size based on the current value of the slider and updates the text field's text format using the setTextFormat() method. */ function sliderChangeHandler(event:SliderEvent):void { myFormat.size = event.value; myTextField.setTextFormat(myFormat); }

Result

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

To get the source files for this example, download embeddingfonts_section5_example2.zip from the top of this page.

Tip: The benefits of advanced anti-aliasing is most apparent with smaller fonts. In the previous example, enable embedded fonts, advanced anti-aliasing, and try setting the font size slider to its lowest value (8 points).

Fading text using embedded fonts and the Tween class

Now that a font symbol is in your Library, you can fade and rotate text and use fonts that aren't on a user's computer.

The following example demonstrates how to use the various tween classes in ActionScript 3.0 to create a rotated text field that fades its text in and out.

Example

The following example uses the Tween class to control the alpha property of a text field.

Note: The previous example requires that a font symbol with the linkage class name of Font1 exists in the library.

// Import the required transition classes. import fl.transitions.Tween; import fl.transitions.TweenEvent; import fl.transitions.easing.*; // Create a new instance of the Font1 symbol from the document's library. var myFont:Font = new Font1(); /* Create a new TextFormat object, and set the font property to the myFont object's fontName property. */ var myFormat:TextFormat = new TextFormat(); myFormat.font = myFont.fontName; myFormat.size = 24; /* Create a new TextField object, assign the text format using the defaultTextFormat property, set the embedFonts property to true, set the antiAliasType property to "advanced", and rotate the text field. */ var myTextField:TextField = new TextField(); myTextField.autoSize = TextFieldAutoSize.LEFT; myTextField.embedFonts = true; myTextField.antiAliasType = AntiAliasType.ADVANCED; myTextField.defaultTextFormat = myFormat; myTextField.text = "The quick brown fox jumped over the lazy dog."; myTextField.border = true; myTextField.x = 15; myTextField.y = 5; myTextField.rotation = 15; addChild(myTextField); // Create a new Tween object which will fade the text field's alpha property. var fadeTween:Tween = new Tween(myTextField, "alpha", Strong.easeIn, 1, 0, 2, true); fadeTween.addEventListener(TweenEvent.MOTION_FINISH, motionFinishHandler); /* Handler for the fade tween. When the tween dispatches the motionFinish event, this function gets called and reverses the direction of the tween. */ function motionFinishHandler(event:TweenEvent):void { Tween(event.currentTarget).yoyo(); }

Result

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

To get the source files for this example, download embeddingfonts_section6_example1.zip from the top of this page.


Related Flash Quick Starts

  • Loading images and Library assets in Flash with ActionScript 3
  • Loading and saving local files with the FileReference class
  • Event handling
  • Display list programming
  • Creating a simple ActionScript 3 class
  • Working with symbols and the Document class
  • Programming with arrays
  • Programming with the Vector class

More Like This

  • Display list programming in ActionScript 3
  • Using ActionScript 3 drawing commands
  • Programming with the Vector class
  • Creating, populating, and resizing the DataGrid component
  • Customizing and sorting the DataGrid component
  • Filtering and formatting data in the DataGrid component
  • Getting started with Flash CS4 user interface components
  • Using the Label component
  • Loading images and Library assets with ActionScript 3
  • Displaying images with the TileList component

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement