Recipe 8.22 Displaying Unicode Text

8.22.1 Problem

You want to display Unicode text in your movie, possibly including non-English characters.

8.22.2 Solution

Use the Unicode escape sequences for the characters within the assignment to the text field's text property. You can also include the Unicode text directly in an external ActionScript file and use an #include directive.

8.22.3 Discussion

All text in Flash MX is treated as Unicode text. Therefore, you can display any Unicode character within a text field. If you know the escape sequence for the character, you can assign it to a text field's text property. All Unicode escape sequences in ActionScript begin with \u and are followed by four-digit hexadecimal numbers. The escape sequences must be enclosed in quotes.

// Display a famous equation with a superscripted 2.
myTextField.text = "E = MC\u00B2";

You can also display Unicode text without using escape sequences by inserting the desired characters directly into an external ActionScript file and using the #include directive to load the text into the Flash movie. This is necessary because Flash does not allow you to enter Unicode characters directly into the Actions panel. You must make sure that the text editor you use allows you to enter Unicode text and save it as UTF-8-encoded format (Notepad and TextEdit both allow for this). You must also include the special //!-- UTF8 comment at the beginning of the file to indicate that it contains UTF-8-encoded text. For example, you can save this text in a file named UTFcontent.as:

//!-- UTF8
// This is the ActionScript file saved as something like UTFcontent.as.
mySpanish = "Español";
myJapanese = "figs/japanese01.gif figs/japanese02.gif";
myHebrew = "figs/hebrew01.gif figs/hebrew02.gif";

Notice that non-English characters can be directly inserted into the external file. Some of these characters might require the user to have the proper language pack installed on his computer.

In the Flash document, include the external file and assign the loaded variables containing Unicode characters to the text properties of existing text fields as desired. For example:

#include "UTFcontent.as"
mySpanishTextField.text = mySpanish;
myJapaneseTextField.text = myJapanese;
myHebrewTextField.text = myHebrew;

8.22.4 See Also

Table A-1 lists the Unicode escape sequences. If you want a Unicode character reference online, you can open up the Character Map utility under Windows using Start Programs Accessories System Tools Character Map.



    Part I: Local Recipes
    Part II: Remote Recipes