13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Using the TextField class<br />

for (var i = 0; i < this.numChildr<strong>en</strong>; i++) {<br />

var displayitem:DisplayObject = this.getChildAt(i);<br />

if (displayitem instanceof StaticText) {<br />

trace("a static text field is item " + i + " on the display list");<br />

var myFieldLabel:StaticText = StaticText(displayitem);<br />

trace("and contains the text: " + myFieldLabel.text);<br />

}<br />

}<br />

Once you have a refer<strong>en</strong>ce to a static text field, you can use the properties of that field in ActionScript 3.0. The following<br />

code is attached to a frame in the Timeline, and assumes that a variable named myFieldLabel is assigned to a static<br />

text refer<strong>en</strong>ce. A dynamic text field named myField is positioned relative to the x and y values of myFieldLabel and<br />

displays the value of myFieldLabel again.<br />

var myField:TextField = new TextField();<br />

addChild(myField);<br />

myField.x = myFieldLabel.x;<br />

myField.y = myFieldLabel.y + 20;<br />

myField.autoSize = TextFieldAutoSize.LEFT;<br />

myField.text = "and " + myFieldLabel.text<br />

Using the TextSnapshot class<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

If you want to programmatically work with an existing static text instance, you can use the flash.text.TextSnapshot<br />

class to work with the textSnapshot property of a flash.display.DisplayObjectContainer. In other words, you create<br />

a TextSnapshot instance from the DisplayObjectContainer.textSnapshot property. You can th<strong>en</strong> apply methods<br />

to that instance to retrieve values or select parts of the static text.<br />

For example, place a static text field that contains the text "TextSnapshot Example" on the Stage. Add the following<br />

ActionScript to Frame 1 of the Timeline:<br />

var mySnap:TextSnapshot = this.textSnapshot;<br />

var count:Number = mySnap.charCount;<br />

mySnap.setSelected(0, 4, true);<br />

mySnap.setSelected(1, 2, false);<br />

var myText:String = mySnap.getSelectedText(false);<br />

trace(myText);<br />

The TextSnapshot class is useful for getting the text out of static text fields in a loaded SWF file, if you want to use the<br />

text as a value in another part of an application.<br />

TextField Example: Newspaper-style text formatting<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

The News Layout example formats text to look something like a story in a printed newspaper. The input text can<br />

contain a headline, a subtitle, and the body of the story. Giv<strong>en</strong> a display width and height, this News Layout example<br />

formats the headline and the subtitle to take the full width of the display area. The story text is distributed across two<br />

or more columns.<br />

Last updated 6/6/2012<br />

387

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!