8.22. Displaying Unicode Text

Problem

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

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.

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 ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.