flash.text.engine

# TextElement - AS3

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

Language Version: | ActionScript 3.0

Runtime Versions: | Flash Player 10, AIR 1.5, Flash Lite 4

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. Assign it to a GroupElement object to combine it with other text and graphic elements as a unit. Use the ElementFormat class to format the text.

Related API Elements

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 object used for the element. | ContentElement
|  | eventMirror : EventDispatcher The EventDispatcher object that receives copies of every event dispatched to valid text lines based on this content element. | ContentElement
|  | groupElement : GroupElement [read-only] The GroupElement object that contains this element, or null if it is not in a group. | ContentElement
|  | rawText : String [read-only] A copy of the text in the element, including any U+FDEF characters. | ContentElement
|  | text : String Receives the text that is the content of the element. | TextElement
|  | textBlock : flash.text.engine: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 an application 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
|  | toLocaleString():String Returns the string representation of this object, formatted according to locale-specific conventions. | 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
--- | --- | ---

Property Detail

### text

property

`text:String`

Language Version: | ActionScript 3.0

Runtime Versions: | Flash Player 10, AIR 1.5, Flash Lite 4

Receives the text that is the content of the element.

The default value is `null`.

Implementation
`public function get text():String`
`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

Runtime Versions: | Flash Player 10, AIR 1.5, Flash Lite 4

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 receives 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 `TextRotation` constants for this property. The default value is `TextRotation.ROTATE_0`.

Example ( How to use this 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 TextBlock. It calls the createLines() function to break the block of text into lines of 150 pixels each.
Copy

    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:String = "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

Runtime Versions: | Flash Player 10, AIR 1.5, Flash Lite 4

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.

More examples

Replacing text
Example ( How to use this 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

Copy

    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);
                format.fontSize = 14;
                var textElement:TextElement = new TextElement(str, format);
                var textBlock:TextBlock = new TextBlock();
                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);
            }
        }
    }

© 2015 Adobe Systems Incorporated. All rights reserved.
Mon Apr 10 2017, 12:28 PM -07:00