ActionScript 3.0 Language and Components Reference | All Packages | All Classes | Language Elements | Index | Appendixes | Conventions | Frames No Frames |
TextElement

Package | flash.text.engine
Class | public final class TextElement
Inheritance | TextElement ContentElement Object

Language Version : | ActionScript 3.0

Player Version : | Flash Player 10

The TextElement class represents a string of formatted text. Assign a TextElement object to the `content` property of a TextBlock object to create a block of text, or assign it to a GroupElement object to combine it with other text and graphic elements. Use the ElementFormat class to format the text.

See also

ContentElement
ElementFormat
TextBlock

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | constructor : Object A reference to the class object or constructor function for a given object instance. | Object
|  | elementFormat : ElementFormat The ElementFormat used for the element. | ContentElement
|  | eventMirror : EventDispatcher The EventDispatcher object that receives copies of every event dispatched to text lines based on this content element. | ContentElement
|  | groupElement : GroupElement [read-only] The GroupElement which contains this element, or null if it is not in a group. | ContentElement
|  | prototype : Object [static] A reference to the prototype object of a class or function object. | Object
|  | rawText : String [read-only] A copy of the text in the element, including the U+FDEF characters representing graphic elements. | ContentElement
|  | text : String [write-only] Receives the text that is the content of the element. | TextElement
|  | textBlock : TextBlock [read-only] The TextBlock to which this element belongs. | ContentElement
|  | textBlockBeginIndex : int [read-only] The index in the text block of the first character of this element. | ContentElement
|  | textRotation : String The rotation to apply to the element as a unit. | ContentElement
|  | userData : * Provides a way for the author to associate arbitrary data with the element. | ContentElement

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | TextElement(text:String = null, elementFormat:ElementFormat = null, eventMirror:EventDispatcher = null, textRotation:String = "rotate0") Creates a new TextElement instance. | TextElement
|  | hasOwnProperty(name:String):Boolean Indicates whether an object has a specified property defined. | Object
|  | isPrototypeOf(theClass:Object):Boolean Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter. | Object
|  | propertyIsEnumerable(name:String):Boolean Indicates whether the specified property exists and is enumerable. | Object
|  | replaceText(beginIndex:int, endIndex:int, newText:String):void Replaces the range of characters that the beginIndex and endIndex parameters specify with the contents of the newText parameter. | TextElement
|  | setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void Sets the availability of a dynamic property for loop operations. | Object
|  | toString():String Returns the string representation of the specified object. | Object
|  | valueOf():Object Returns the primitive value of the specified object. | Object

Public Constants
Hide Inherited Public Constants
Show Inherited Public Constants

| Constant | Defined By
--- | --- | ---
|  | GRAPHIC_ELEMENT : uint = 0xFDEF [static] Used to indicate the presence a graphic element in the text. | ContentElement

Property Detail

text | property

`text:String` [write-only]

Language Version : | ActionScript 3.0

Player Version : | Flash Player 10

Receives the text that is the content of the element.

The default value is `null`.

Implementation
`public function set text(value:String):void`
Constructor Detail

TextElement | () | Constructor

`public function TextElement(text:String = null, elementFormat:ElementFormat = null, eventMirror:EventDispatcher = null, textRotation:String = "rotate0")`

Language Version : | ActionScript 3.0

Player Version : | Flash Player 10

Creates a new TextElement instance.

Parameters

| `text:String` (default = `null`)`` — The text for the element. The default value is `null`.
| `elementFormat:ElementFormat` (default = `null`)`` — The element format for the text in the element. The default value is `null`.
| `eventMirror:EventDispatcher` (default = `null`)`` — The `EventDispatcher` object that will receive copies of every event dispatched to text lines based on this content element. The default value is `null`.
| `textRotation:String` (default = "`rotate0`")`` — The rotation applied the element as a unit. Use `flash.text.engine.TextRotation` constants for this property. The default value is `flash.text.engine.TextRotation.ROTATE_0`.

Example
The following example creates a TextElement object from a string of text, formats it using a font size of 12 and the color red (0xCC0000), and assigns it to the `content` property of a TextextBlocklock. It calls the createLines() function to break the block of text into lines of 150 pixels each.

    package {
        import flash.display.Sprite;
        import flash.text.engine.TextBlock;
        import flash.text.engine.TextElement;
        import flash.text.engine.TextLine;
        import flash.text.engine.ElementFormat;

        public class TextElementExample extends Sprite {

            public function TextElementExample():void {
                var str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " +
                "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut " +
                "enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut " +
                "aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit " +
                "in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur " +
                "sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt " +
                "mollit anim id est laborum.";
                var format:ElementFormat = new ElementFormat(null, 12, 0xCC0000);
                var textElement:TextElement = new TextElement(str, format);
                var textBlock:TextBlock = new TextBlock();
                textBlock.content = textElement;
                createLines(textBlock);
            }
            private function createLines(textBlock:TextBlock):void {

                var yPos = 20;
                var textLine:TextLine = textBlock.createTextLine (null, 150);

                while (textLine)
                {
                    addChild(textLine);
                    textLine.x = 15;
                    yPos += textLine.textHeight+2;
                    textLine.y = yPos;
                    textLine = textBlock.createTextLine(textLine, 150);
                }
            }
        }
    }

Method Detail

replaceText | () | method

`public function replaceText(beginIndex:int, endIndex:int, newText:String):void`

Language Version : | ActionScript 3.0

Player Version : | Flash Player 10

Replaces the range of characters that the `beginIndex` and `endIndex` parameters specify with the contents of the `newText` parameter. The `beginIndex` and `endIndex` values refer to the current contents of `text`.

To delete text, pass `null` for `newText`.

To insert text, pass the same value for `beginIndex` and `endIndex`. The new text is inserted before the specified index.

To append text, pass `text.length` for `beginIndex` and `endIndex`.

To set all the text, pass 0 for `beginIndex` and `text.length` for `endIndex`.

Parameters

| `beginIndex:int` — The zero-based index value for the start position of the replacement range.
| `endIndex:int` — The zero-based index value following the end position of the replacement range.
| `newText:String` — The text to use to replace the specified range of characters.

Throws

| `RangeError` — The `beginIndex` or `endIndex` specified is out of range.

Example
This example calls `replaceText()` several times to do the following:

- insert a string at the beginning of text
- append a string to the end of text
- insert a string in the middle of text
- replace text entirely with new text

    package {
        import flash.display.Sprite;
        import flash.text.engine.FontDescription;
        import flash.text.engine.ElementFormat;
        import flash.text.engine.TextElement;
        import flash.text.engine.TextBlock;
        import flash.text.engine.TextLine;

        public class TextElement_replaceTextExample extends Sprite {
            public function TextElement_replaceTextExample():void {

                var str:String = "0123456";
                var fontDescription:FontDescription = new FontDescription("Arial");
                var format:ElementFormat = new ElementFormat(fontDescription);
                var textElement:TextElement = new TextElement(str, format);
                var textBlock:TextBlock = new TextBlock();
                format.fontSize = 14;
                textBlock.content = textElement;
                textElement.replaceText(0, 0, "abc");
                createLine(textBlock, 20);           //"abc0123456"
                textElement.replaceText(10, 10, "abc");
                createLine(textBlock, 40);            // "abc0123456abc"
                textElement.replaceText(5, 8, "abc");
                createLine(textBlock, 60);         // "abc01abc56abc"
                textElement.replaceText(0, 13, "abc");
                createLine(textBlock, 80);            // "abc"
                textElement.replaceText(0, 3, "That's all she wrote!");
                createLine(textBlock, 100);        // "That's all she wrote" */
            }

            private function createLine(textBlock:TextBlock, y:Number):void {
                var textLine:TextLine = textBlock.createTextLine(null, 150);
                textLine.x = 10;
                textLine.y = y;
                addChild(textLine);
            }
        }
    }

Submit Feedback on LiveDocs
© 2004-2008 Adobe Systems Incorporated. All rights reserved.
Tue May 20 2008, 04:02 AM -07:00