Adobe® Flex® 4 Language Reference
Hide Packages and Classes List |  Packages  |  Classes  |  Index  |  Appendixes
flash.text.engine 
TextElement 
Packageflash.text.engine
Classpublic final class TextElement
InheritanceTextElement Inheritance ContentElement Inheritance Object

Language Version: ActionScript 3.0
Runtime Versions: Flash Player 10, AIR 1.5

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.

See also



Public Properties
 PropertyDefined By
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
 InheritedelementFormat : ElementFormat
The ElementFormat object used for the element.
ContentElement
 InheritedeventMirror : EventDispatcher
The EventDispatcher object that receives copies of every event dispatched to valid text lines based on this content element.
ContentElement
 InheritedgroupElement : GroupElement
[read-only] The GroupElement object that contains this element, or null if it is not in a group.
ContentElement
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
 InheritedrawText : String
[read-only] A copy of the text in the element, including the U+FDEF characters.
ContentElement
  text : String
Receives the text that is the content of the element.
TextElement
 InheritedtextBlock : flash.text.engine:TextBlock
[read-only] The TextBlock to which this element belongs.
ContentElement
 InheritedtextBlockBeginIndex : int
[read-only] The index in the text block of the first character of this element.
ContentElement
 InheritedtextRotation : String
The rotation to apply to the element as a unit.
ContentElement
 InheriteduserData : *
Provides a way for the author to associate arbitrary data with the element.
ContentElement
Public Methods
 MethodDefined By
  
TextElement(text:String = null, elementFormat:ElementFormat = null, eventMirror:EventDispatcher = null, textRotation:String = "rotate0")
Creates a new TextElement instance.
TextElement
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
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
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
 Inherited
Returns the string representation of this object, formatted according to locale-specific conventions.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
Public Constants
 ConstantDefined By
 InheritedGRAPHIC_ELEMENT : uint = 0xFDEF
[static] Indicates the presence a graphic element in the text.
ContentElement
Property Detail

text

property
text:String

Language Version: ActionScript 3.0
Runtime Versions: Flash Player 10, AIR 1.5

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

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 flash.text.engine.TextRotation constants for this property. The default value is flash.text.engine.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.

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

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  ( 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

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);
        }
    }
}