包 | flashx.textLayout.events |
类 | public class CompositionCompleteEvent |
继承 | CompositionCompleteEvent Event Object |
语言版本: | ActionScript 3.0 |
运行时版本: | Flash Player 10, AIR 1.5 |
调度此事件的三个主要方法是 compose()
、updateToController()
和 updateAllControllers()
。这三个方法都属于 StandardFlowComposer 类。
注意:如果事件是由 updateAllControllers()
方法调度的,则不要在事件处理函数中再次调用 updateAllControllers()
。这样的调用会是递归调用,因为 updateAllControllers()
方法执行合成和显示步骤。updateAllControllers()
在合成完成之后,显示步骤执行之前调度此事件。相同的推理适用于 updateToController()
方法。
相关 API 元素
公共属性
属性 | 由以下参数定义 | ||
---|---|---|---|
bubbles : Boolean [只读]
表示事件是否为冒泡事件。 | Event | ||
cancelable : Boolean [只读]
表示是否可以阻止与事件相关联的行为。 | Event | ||
compositionLength : int
合成的字符数。 | CompositionCompleteEvent | ||
compositionStart : int
受合成影响的文本范围的起始位置,以文本流中的索引表示。 | CompositionCompleteEvent | ||
constructor : Object
对类对象或给定对象实例的构造函数的引用。 | Object | ||
currentTarget : Object [只读]
当前正在使用某个事件侦听器处理 Event 对象的对象。 | Event | ||
eventPhase : uint [只读]
事件流中的当前阶段。 | Event | ||
target : Object [只读]
事件目标。 | Event | ||
textFlow : flashx.textLayout.elements:TextFlow
在其上已完成合成的 TextFlow。 | CompositionCompleteEvent | ||
type : String [只读]
事件的类型。 | Event |
公共方法
方法 | 由以下参数定义 | ||
---|---|---|---|
CompositionCompleteEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, textFlow:flashx.textLayout.elements:TextFlow = null, compositionStart:int = 0, compositionLength:int = 0) 构造函数
| CompositionCompleteEvent | ||
复制 Event 子类的实例。 | Event | ||
用于在自定义 ActionScript 3.0 Event 类中实现 toString() 方法的实用程序函数。 | Event | ||
表示对象是否已经定义了指定的属性。 | Object | ||
检查是否已对事件调用 preventDefault() 方法。 | Event | ||
表示 Object 类的实例是否在指定为参数的对象的原型链中。 | Object | ||
如果可以取消事件的默认行为,则取消该行为。 | Event | ||
表示指定的属性是否存在、是否可枚举。 | Object | ||
设置循环操作动态属性的可用性。 | Object | ||
防止对事件流中当前节点中和所有后续节点中的事件侦听器进行处理。 | Event | ||
防止对事件流中当前节点的后续节点中的所有事件侦听器进行处理。 | Event | ||
返回此对象的字符串表示形式,其格式设置遵守区域设置特定的约定。 | Object | ||
返回一个字符串,其中包含 Event 对象的所有属性。 | Event | ||
返回指定对象的原始值。 | Object |
公共常量
常量 | 由以下参数定义 | ||
---|---|---|---|
COMPOSITION_COMPLETE : String = "compositionComplete" [静态]
定义 compositionComplete 事件对象的 type 属性值。
| CompositionCompleteEvent |
属性详细信息
compositionLength | 属性 |
compositionStart | 属性 |
textFlow | 属性 |
textFlow:flashx.textLayout.elements:TextFlow
语言版本: | ActionScript 3.0 |
运行时版本: | Flash Player 10, AIR 1.5 |
在其上已完成合成的 TextFlow。
实现
public function get textFlow():flashx.textLayout.elements:TextFlow
public function set textFlow(value:flashx.textLayout.elements:TextFlow):void
构造函数详细信息
CompositionCompleteEvent | () | 构造函数 |
public function CompositionCompleteEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, textFlow:flashx.textLayout.elements:TextFlow = null, compositionStart:int = 0, compositionLength:int = 0)
语言版本: | ActionScript 3.0 |
运行时版本: | Flash Player 10, AIR 1.5 |
构造函数
参数type:String — 事件类型 - 使用静态属性 COMPOSITION_COMPLETE。
| |
bubbles:Boolean (default = false ) — 表示事件是否为冒泡事件。该事件不会冒泡。
| |
cancelable:Boolean (default = false ) — 表示是否可以阻止与事件相关联的行为。无法取消此事件。
| |
textFlow:flashx.textLayout.elements:TextFlow (default = null ) — 被合成的 TextFlow
| |
compositionStart:int (default = 0 ) — 合成开始位置,以文本流内的索引表示。
| |
compositionLength:int (default = 0 ) — 合成的字符长度值
|
常量详细信息
COMPOSITION_COMPLETE | 常量 |
public static const COMPOSITION_COMPLETE:String = "compositionComplete"
语言版本: | ActionScript 3.0 |
运行时版本: | Flash Player 10, AIR 1.5 |
定义 compositionComplete
事件对象的 type
属性值。
示例 如何使用本示例
CompositionCompleteEvent_example.as
此示例说明如何使用 CompositionCompleteEvent 类加亮 TextFlow 中的特定词。此方法表示在合成文本之前设置 TextLayoutFormat.backgroundColor
属性的另一种方法。
compositionCompleteHandler()
方法在 updateAllControllers()
方法启动执行(通过合成文本)之后,updateAllControllers()
完成执行(通过更新显示列表)之前执行。这使得事件处理函数方法能够在 updateAllControllers()
更新显示列表之前绘制词“shall”的加亮效果。
package flashx.textLayout.events.examples { import flash.display.Graphics; import flash.display.Sprite; import flash.geom.Rectangle; import flash.text.engine.TextLine; import flashx.textLayout.compose.StandardFlowComposer; import flashx.textLayout.compose.TextFlowLine; import flashx.textLayout.container.ContainerController; import flashx.textLayout.conversion.TextConverter; import flashx.textLayout.elements.FlowLeafElement; import flashx.textLayout.elements.ParagraphElement; import flashx.textLayout.elements.TextFlow; import flashx.textLayout.events.CompositionCompleteEvent; public class CompositionCompleteEvent_example extends Sprite { private var wordToHighlight:String = "shall"; private var poemContainer:Sprite; private var txtFlow:TextFlow; private var controller:ContainerController; private const poem:String = "Sonnet 18\n" + "Shall I compare thee to a summer's day?\n" + "Thou art more lovely and more temperate:\n" + "Rough winds do shake the darling buds of May,\n" + "And summer's lease hath all too short a date:\n" + "Sometime too hot the eye of heaven shines,\n" + "And often is his gold complexion dimmed,\n" + "And every fair from fair sometime declines,\n" + "By chance, or nature's changing course untrimmed:\n" + "But thy eternal summer shall not fade,\n" + "Nor lose possession of that fair thou ow'st,\n" + "Nor shall death brag thou wand'rest in his shade,\n" + "When in eternal lines to time thou grow'st,\n" + "So long as men can breathe or eyes can see,\n" + "So long lives this, and this gives life to thee.\n" + "William Shakespeare\n"; public function CompositionCompleteEvent_example() { poemContainer = new Sprite(); txtFlow = new TextFlow(); poemContainer.x = 30; poemContainer.y = 30; this.stage.addChild( poemContainer ); controller = new ContainerController(poemContainer, 300, 230); controller.verticalScrollPolicy = flashx.textLayout.container.ScrollPolicy.OFF; txtFlow = TextConverter.importToFlow( poem, TextConverter.PLAIN_TEXT_FORMAT ); txtFlow.flowComposer = new StandardFlowComposer(); txtFlow.addEventListener(CompositionCompleteEvent.COMPOSITION_COMPLETE, compositionCompleteHandler); txtFlow.flowComposer.addController( controller ); txtFlow.flowComposer.updateAllControllers(); } private function compositionCompleteHandler(event:CompositionCompleteEvent):void { if (event.textFlow == txtFlow) { checkForMatchingWord(); } } private function checkForMatchingWord():void { var currentLeaf:FlowLeafElement = txtFlow.getFirstLeaf(); var currentParagraph:ParagraphElement = currentLeaf ? currentLeaf.getParagraph() : null; while (currentParagraph) { // iterate over all paragraphs in the text flow var paraStart:uint = currentParagraph.getAbsoluteStart(); var currWordBoundary:int = 0; var nextWordBoundary:int = 0; var pattern:RegExp = new RegExp(wordToHighlight, "i"); while (true) { // iterate over letters in a word nextWordBoundary = currentParagraph.findNextWordBoundary(currWordBoundary); if (nextWordBoundary == currWordBoundary) { break; // end of paragraph; break of inner while loop } var word:String = ''; var indexInLeaf:int = currWordBoundary + paraStart - currentLeaf.getAbsoluteStart(); var wordLen:uint = nextWordBoundary - currWordBoundary; while (true){ // traverse consecutive leaf elements looking for 'wordLen' characters // Take as many characters from the current leaf as possible to meet the quota of 'wordLen' var consumeCount:uint = indexInLeaf + wordLen <= currentLeaf.textLength ? wordLen : currentLeaf.textLength - indexInLeaf; word += currentLeaf.text.substr (indexInLeaf, consumeCount); wordLen -= consumeCount; if (!wordLen) break; // Quota not met; move to the next leaf // Also reset the index where the next leaf will be scanned currentLeaf = currentLeaf.getNextLeaf(); indexInLeaf = 0; } if (pattern.test(word)) { highlightWord(currWordBoundary, nextWordBoundary, word, paraStart); } currWordBoundary = nextWordBoundary; } // At this point, currentLeaf is the last leaf in currentParagraph. Move to the next paragraph. currentLeaf = currentLeaf.getNextLeaf(); currentParagraph = currentLeaf ? currentLeaf.getParagraph() : null; } } private function highlightWord(begin:int, end:int, word:String, paraStart:int):void { var absoluteBegin:int = paraStart + begin; var absoluteEnd:int = paraStart + end; var startTextFlowLineIndex:int = txtFlow.flowComposer.findLineIndexAtPosition(absoluteBegin); var endTextFlowLineIndex:int = txtFlow.flowComposer.findLineIndexAtPosition(absoluteEnd); var txtFlowLine:TextFlowLine = txtFlow.flowComposer.getLineAt(startTextFlowLineIndex); var txtLine:TextLine = txtFlowLine.getTextLine(); // get the bounds of the first and last Atom of the textLine var startbounds:Rectangle = txtLine.getAtomBounds(txtLine.getAtomIndexAtCharIndex(begin)); var endbounds:Rectangle = txtLine.getAtomBounds(txtLine.getAtomIndexAtCharIndex(end)); var box:Sprite = new Sprite(); var g:Graphics = box.graphics; g.lineStyle(1, 0xFFFF00, 0.5); // set line style g.beginFill(0xFFFF00, 0.3); // set fill style // draw a rectangle around the specified boundaries g.drawRect(startbounds.left, startbounds.top, (endbounds.x - startbounds.x), startbounds.height); txtLine.addChild(box); } } }
Tue Jun 12 2018, 11:04 AM Z