包flash.text.engine
类public final class TextElement
继承TextElement Inheritance ContentElement Inheritance Object

语言版本: ActionScript 3.0
运行时版本: Flash Player 10, AIR 1.5

TextElement 类表示已设置格式的文本的字符串。将 TextElement 对象分配给 TextBlock 对象的 content 属性以创建文本块。将其作为一个单元分配给 GroupElement 对象以与其他文本和图形元素结合使用。使用 ElementFormat 类设置文本的格式。

另请参见

ContentElement
ElementFormat
TextBlock


公共属性
 属性定义方
 Inheritedconstructor : Object
对类对象或给定对象实例的构造函数的引用。
Object
 InheritedelementFormat : ElementFormat
用于元素的 ElementFormat 对象。
ContentElement
 InheritedeventMirror : EventDispatcher
EventDispatcher 对象,该对象将接收调度到基于此内容元素的有效文本行的每个事件的副本。
ContentElement
 InheritedgroupElement : GroupElement
[只读 (read-only)] 包含此元素的 GroupElement 对象;如果此元素不在一个组中,则为 null。
ContentElement
 Inheritedprototype : Object
[静态] 对类或函数对象的原型对象的引用。
Object
 InheritedrawText : String
[只读 (read-only)] 元素中的文本的副本,包括 U+FDEF 字符。
ContentElement
  text : String
[只写] 接收作为元素内容的文本。
TextElement
 InheritedtextBlock : TextBlock
[只读 (read-only)] 此元素所属的 TextBlock。
ContentElement
 InheritedtextBlockBeginIndex : int
[只读 (read-only)] 文本块中此元素的第一个字符的索引。
ContentElement
 InheritedtextRotation : String
应用于元素的旋转(旋转时将元素作为一个单元)。
ContentElement
 InheriteduserData : *
为作者提供了一种将任意数据与元素相关联的途径。
ContentElement
公共方法
 方法定义方
  
TextElement(text:String = null, elementFormat:ElementFormat = null, eventMirror:EventDispatcher = null, textRotation:String = "rotate0")
创建新的 TextElement 实例。
TextElement
 Inherited
指示对象是否已经定义了指定的属性。
Object
 Inherited
指示 Object 类的实例是否在指定为参数的对象的原型链中。
Object
 Inherited
指示指定的属性是否存在、是否可枚举。
Object
  
replaceText(beginIndex:int, endIndex:int, newText:String):void
将 beginIndex 和 endIndex 参数指定的字符范围替换为 newText 参数的内容。
TextElement
 Inherited
设置循环操作动态属性的可用性。
Object
 Inherited
返回指定对象的字符串表示形式。
Object
 Inherited
返回指定对象的原始值。
Object
公共常量
 常量定义方
 InheritedGRAPHIC_ELEMENT : uint = 0xFDEF
[静态] 指示文本中存在图形元素。
ContentElement
属性详细信息
text属性
text:String  [只写]

语言版本: ActionScript 3.0
运行时版本: Flash Player 10, AIR 1.5

接收作为元素内容的文本。

默认值为 null。



实现
    public function set text(value:String):void
构造函数详细信息
TextElement()构造函数
public function TextElement(text:String = null, elementFormat:ElementFormat = null, eventMirror:EventDispatcher = null, textRotation:String = "rotate0")

语言版本: ActionScript 3.0
运行时版本: Flash Player 10, AIR 1.5

创建新的 TextElement 实例。

参数
text:String (default = null) — 元素的文本。默认值为 null。
 
elementFormat:ElementFormat (default = null) — 元素中文本的元素格式。默认值为 null。
 
eventMirror:EventDispatcher (default = null) — EventDispatcher 对象,该对象将接收调度到基于此内容元素的文本行的每个事件的副本。默认值为 null。
 
textRotation:String (default = "rotate0") — 作为一个单元应用于元素的旋转。对此属性使用 flash.text.engine.TextRotation 常量。默认值为 flash.text.engine.TextRotation.ROTATE_0。

示例

以下示例将通过文本字符串创建 TextElement 对象、使用 12 号字体和红色 (0xCC0000) 来设置该对象的格式,并将其分配给 TextBlock 的 content 属性。它调用 createLines() 函数将文本块分成多行(每行为 150 像素)。
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);
            }        
        }
    }
}
方法详细信息
replaceText()方法
public function replaceText(beginIndex:int, endIndex:int, newText:String):void

语言版本: ActionScript 3.0
运行时版本: Flash Player 10, AIR 1.5

将 beginIndex 和 endIndex 参数指定的字符范围替换为 newText 参数的内容。beginIndex 和 endIndex 值是指 text 的当前内容。

若要删除文本,请为 newText 传递 null。

若要插入文本,请为 beginIndex 和 endIndex 传递相同的值。新文本将插入到指定索引之前。

若要追加文本,请为 beginIndex 和 endIndex 传递 text.length。

若要设置所有文本,请为 beginIndex 传递 0,为 endIndex 传递 text.length。

参数

beginIndex:int — 替换范围开始位置的从零开始的索引值。
 
endIndex:int — 替换范围结束位置后面从零开始的索引值。
 
newText:String — 要用来替换指定范围字符的文本。


引发
RangeError — 指定的 beginIndex 或 endIndex 超出范围。

示例

为了执行下列操作,此示例将多次调用 replaceText():
  • 在文本开头插入字符串
  • 向文本末尾追加字符串
  • 在文本中间插入字符串
  • 将文本全部替换为新文本
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);
        }
    }
}