| 包 | flash.text.engine |
| 类 | public final class TextElement |
| 继承 | TextElement ContentElement Object |
| 语言版本: | ActionScript 3.0 |
| 运行时版本: | Flash Player 10, AIR 1.5, Flash Lite 4 |
content 属性以创建文本块。将其作为一个单元分配给 GroupElement 对象以与其它文本和图形元素结合使用。使用 ElementFormat 类设置文本的格式。
相关 API 元素
公共属性
| 属性 | 由以下参数定义 | ||
|---|---|---|---|
![]() | constructor : Object
对类对象或给定对象实例的构造函数的引用。 | Object | |
![]() | elementFormat : ElementFormat
用于元素的 ElementFormat 对象。 | ContentElement | |
![]() | eventMirror : EventDispatcher
EventDispatcher 对象,该对象将接收调度到基于此内容元素的有效文本行的每个事件的副本。 | ContentElement | |
![]() | groupElement : GroupElement [只读]
包含此元素的 GroupElement 对象;如果此元素不在一个组中,则为 null。 | ContentElement | |
![]() | rawText : String [只读]
元素中文本的副本,包括任何 U+FDEF 字符。 | ContentElement | |
| text : String
接收作为元素内容的文本。 | TextElement | ||
![]() | textBlock : flash.text.engine:TextBlock [只读]
此元素所属的 TextBlock。 | ContentElement | |
![]() | textBlockBeginIndex : int [只读]
文本块中此元素的第一个字符的索引。 | 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 | |
![]() |
返回指定对象的原始值。 | Object | |
属性详细信息
text | 属性 |
构造函数详细信息
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, Flash Lite 4 |
创建新的 TextElement 实例。
参数text:String (default = null) — 元素的文本。默认值为 null。
| |
elementFormat:ElementFormat (default = null) — 元素中文本的元素格式。默认值为 null。
| |
eventMirror:EventDispatcher (default = null) — EventDispatcher 对象,该对象将接收调度到基于此内容元素的文本行的每个事件的副本。默认值为 null。
| |
textRotation:String (default = "rotate0") — 作为一个单元应用于元素的旋转。将 TextRotation 常量用于此属性。默认值为 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, Flash Lite 4 |
将 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);
}
}
}
Tue Jun 12 2018, 11:04 AM Z
隐藏继承的公共属性
显示继承的公共属性