Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Working with Text and Strings > About text fields > Assigning text to a text field at runtime | |||
When you build applications with Flash, you may want to load text from an external source, such as a text file, an XML file, or even a remote web service. Flash provides a great deal of control over how you create and display text on the Stage, such as supporting text that is HTML formatted, plain text, XML formatted text, and external style sheets. Or you can use ActionScript to define a stylesheet.
To assign text to a text field, you can do one of the following: use the text or htmlTEXT properties; create a variable name for the text field in the Var: field in the Properties inspector and assign text to that; assign a value by binding the text field to a text field in another component.
The following exercise assigns text to a text field at runtime.
Instance names must consist only of letters, numbers, underscores (_), and dollar signs ($).
headline_txt.text = "New articles available on Developer Center";
You can also create a text field with ActionScript, and then assign text to it. Type the following ActionScript on Frame 1 of the Timeline:
this.createTextField("headline_txt", this.getNextHighestDepth(), 100, 100, 300, 20);
headline_txt.text = "New articles available on Developer Center";
This code creates a new text field with the instance name headline_txt. The text field is created at the next highest depth, at the x and y coordinates of 100, 100, with a text field width of 200 pixels and a height of 20 pixels. When you test the SWF file (Control > Test Movie), the text "New articles available on Developer Center" appears on the Stage.
Use one of the following two steps to enable HTML formatting for the text field:
html property to true by using ActionScript (see the following code sample).To apply HTML formatting to a text field by using ActionScript, type the following ActionScript on Frame 1 of the Timeline:
this.createTextField("headline_txt", this.getNextHighestDepth(), 100, 100, 300, 20);
headline_txt.html = true;
headline_txt.htmlText = "New articles available on <i>Developer Center</i>.";
The preceding code dynamically creates a new text field, enables HTML formatting, and displays the text "New articles available on Developer Center" on the Stage, with the word "Developer Center" appearing in italics.
|
CAUTION |
|
When you use HTML formatted text with a text field (not components) on the Stage, you must assign the text to the text field's |
For samples that demonstrate how to work with text fields using ActionScript, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. Download the Samples zip file and navigate to the ActionScript 2.0/TextFields folder to access these samples:
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000880.html