O'Reilly logo

ActionScript 3.0 Cookbook by Joey Lott, Darron Schall, Keith Peters

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Working with Advanced Text Layout

Problem

You want to work with advanced text layout.

Solution

Use the numLines property and the getCharBoundaries(), getCharIndexAtPoint(), getFirstCharInParagraph(), getLineIndexAtPoint(), getLineIndexOfChar(), getLine- Length(), getLineMetrics(), getLineOffset(), getLineText(), and getParagraphLength() methods.

Discussion

In versions of Flash Player up to and including Flash Player 8, it was difficult to control and read text layout with much precision. Starting with Flash Player 8.5, though, the TextField class defines an API for more precise reading of text layout.

The TextField class defines two methods for retrieving information about characters from text. The getCharBoundaries() method returns a flash.geom.Rectangle object that defines the boundaries of the character at the index specified by the parameter. The getCharIndexAtPoint() method returns the index of a character at the and y coordinates specified by the parameters. The following example uses getCharIndexAtPoint() and getCharBoundaries() to highlight a character when the user clicks on it:

package { import flash.display.Sprite; import flash.text.TextField; import flash.events.MouseEvent; import flash.geom.Rectangle; public class Text extends Sprite { private var _field:TextField; private var _highlight:Sprite; public function Text() { _field = new TextField(); _field.border = true; _field.background = true; _field.multiline = true; _field.wordWrap = true; _field.selectable ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required