套件 | 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 類別來反白 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, 03:47 PM Z