| 包 | flash.text.engine |
| 类 | public final class TextElement |
| 继承 | TextElement ContentElement Object |
| 语言版本: | ActionScript 3.0 |
| 运行时版本: | Flash Player 10, AIR 1.5 |
content 属性以创建文本块。将其作为一个单元分配给 GroupElement 对象以与其他文本和图形元素结合使用。使用 ElementFormat 类设置文本的格式。
另请参见
| 属性 | 定义方 | ||
|---|---|---|---|
![]() | constructor : Object
对类对象或给定对象实例的构造函数的引用。 | Object | |
![]() | elementFormat : ElementFormat 用于元素的 ElementFormat 对象。 | ContentElement | |
![]() | eventMirror : EventDispatcher EventDispatcher 对象,该对象将接收调度到基于此内容元素的有效文本行的每个事件的副本。 | ContentElement | |
![]() | groupElement : GroupElement [只读 (read-only)] 包含此元素的 GroupElement 对象;如果此元素不在一个组中,则为 null。 | ContentElement | |
![]() | prototype : Object [静态]
对类或函数对象的原型对象的引用。 | Object | |
![]() | rawText : String [只读 (read-only)] 元素中的文本的副本,包括 U+FDEF 字符。 | ContentElement | |
| text : String [只写] 接收作为元素内容的文本。 | TextElement | ||
![]() | textBlock : TextBlock [只读 (read-only)] 此元素所属的 TextBlock。 | ContentElement | |
![]() | textBlockBeginIndex : int [只读 (read-only)] 文本块中此元素的第一个字符的索引。 | ContentElement | |
![]() | textRotation : String 应用于元素的旋转(旋转时将元素作为一个单元)。 | ContentElement | |
![]() | userData : * 为作者提供了一种将任意数据与元素相关联的途径。 | ContentElement | |
| 方法 | 定义方 | ||
|---|---|---|---|
TextElement(text:String = null, elementFormat:ElementFormat = null, eventMirror:EventDispatcher = null, textRotation:String = "rotate0") 创建新的 TextElement 实例。 | TextElement | ||
![]() |
指示对象是否已经定义了指定的属性。 | Object | |
![]() |
指示 Object 类的实例是否在指定为参数的对象的原型链中。 | Object | |
![]() |
指示指定的属性是否存在、是否可枚举。 | Object | |
将 beginIndex 和 endIndex 参数指定的字符范围替换为 newText 参数的内容。 | TextElement | ||
![]() |
设置循环操作动态属性的可用性。 | Object | |
![]() |
返回指定对象的字符串表示形式。 | Object | |
![]() |
返回指定对象的原始值。 | Object | |
| 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。
|
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);
}
}
}