处理 FTE 中的事件

Flash Player 10 和更高版本,Adobe AIR 1.5 和更高版本

您可以将事件侦听器添加到 TextLine 实例中,就像添加到其他显示对象一样。例如,您可以检测用户何时将鼠标移动到某个文本行的上方,或者用户何时单击此行。下面的示例检测这两种事件。如果将鼠标移动到此行的上方,光标将变为按钮光标,并且如果单击此行,此行将会变色。
package 
{ 
    import flash.text.engine.*; 
    import flash.ui.Mouse; 
    import flash.display.Sprite 
    import flash.events.MouseEvent; 
    import flash.events.EventDispatcher; 
     
    public class EventHandlerExample extends Sprite 
    { 
        var textBlock:TextBlock = new TextBlock(); 
 
        public function EventHandlerExample():void 
        { 
            var str:String = "I'll change color if you click me."; 
            var fontDescription:FontDescription = new FontDescription("Arial"); 
            var format:ElementFormat = new ElementFormat(fontDescription, 18); 
            var textElement = new TextElement(str, format); 
            textBlock.content = textElement; 
            createLine(textBlock); 
        } 
     
        private function createLine(textBlock:TextBlock):void 
        { 
            var textLine:TextLine = textBlock.createTextLine(null, 500); 
            textLine.x = 30; 
            textLine.y = 30; 
            addChild(textLine); 
            textLine.addEventListener("mouseOut", mouseOutHandler); 
            textLine.addEventListener("mouseOver", mouseOverHandler); 
            textLine.addEventListener("click", clickHandler); 
        } 
 
        private function mouseOverHandler(event:MouseEvent):void 
        { 
            Mouse.cursor = "button"; 
        } 
 
        private function mouseOutHandler(event:MouseEvent):void 
        { 
            Mouse.cursor = "arrow"; 
        } 
 
        function clickHandler(event:MouseEvent):void { 
            if(textBlock.firstLine) 
                removeChild(textBlock.firstLine); 
            var newFormat:ElementFormat = textBlock.content.elementFormat.clone(); 
            switch(newFormat.color) 
            { 
                case 0x000000: 
                    newFormat.color = 0xFF0000; 
                    break; 
                case 0xFF0000: 
                    newFormat.color = 0x00FF00; 
                    break; 
                case 0x00FF00: 
                    newFormat.color = 0x0000FF; 
                    break; 
                case 0x0000FF: 
                    newFormat.color = 0x000000; 
                    break; 
            } 
            textBlock.content.elementFormat = newFormat; 
            createLine(textBlock); 
        } 
    } 
}

镜像事件

另外,还可将文本块或文本块局部的事件映射到事件调度程序。首先,创建一个 EventDispatcher 实例,然后将其分配给 TextElement 实例的 eventMirror 属性。如果文本块由一个文本元素组成,那么文本引擎将映射整个文本块的事件。如果文本块由多个文本元素组成,那么文本引擎只映射设置了 eventMirror 属性的 TextElement 实例的事件。下面示例中的文本由三个元素组成:单词“Click”、单词“here”以及字符串“to see me in italic”。此示例将事件调度程序分配给第二个文本元素,即单词“here”,然后添加事件侦听器 clickHandler() 方法。该 clickHandler() 方法将文本更改为斜体。另外,它还将第三个文本元素的内容替换为“Click here to see me in normal font!”。

package 
{ 
    import flash.text.engine.*; 
    import flash.ui.Mouse; 
    import flash.display.Sprite; 
    import flash.events.MouseEvent; 
    import flash.events.EventDispatcher; 
     
    public class EventMirrorExample extends Sprite 
    {             
        var fontDescription:FontDescription = new FontDescription("Helvetica", "bold"); 
        var format:ElementFormat = new ElementFormat(fontDescription, 18); 
        var textElement1 = new TextElement("Click ", format); 
        var textElement2 = new TextElement("here ", format); 
        var textElement3 = new TextElement("to see me in italic! ", format); 
        var textBlock:TextBlock = new TextBlock(); 
 
        public function EventMirrorExample() 
        { 
            var myEvent:EventDispatcher = new EventDispatcher(); 
             
            myEvent.addEventListener("click", clickHandler); 
            myEvent.addEventListener("mouseOut", mouseOutHandler); 
            myEvent.addEventListener("mouseOver", mouseOverHandler); 
             
            textElement2.eventMirror=myEvent; 
             
            var groupVector:Vector.<ContentElement> = new Vector.<ContentElement>; 
            groupVector.push(textElement1, textElement2, textElement3); 
            var groupElement:GroupElement = new GroupElement(groupVector); 
             
            textBlock.content = groupElement; 
            createLines(textBlock); 
        } 
             
        private function clickHandler(event:MouseEvent):void 
        { 
            var newFont:FontDescription = new FontDescription(); 
            newFont.fontWeight = "bold"; 
         
            var newFormat:ElementFormat = new ElementFormat(); 
            newFormat.fontSize = 18; 
            if(textElement3.text == "to see me in italic! ") { 
                newFont.fontPosture = FontPosture.ITALIC; 
                textElement3.replaceText(0,21, "to see me in normal font! "); 
            } 
            else { 
                newFont.fontPosture = FontPosture.NORMAL; 
                textElement3.replaceText(0, 26, "to see me in italic! "); 
            } 
            newFormat.fontDescription = newFont; 
            textElement1.elementFormat = newFormat; 
            textElement2.elementFormat = newFormat; 
            textElement3.elementFormat = newFormat; 
            createLines(textBlock); 
        } 
             
        private function mouseOverHandler(event:MouseEvent):void 
        { 
            Mouse.cursor = "button"; 
        } 
             
        private function mouseOutHandler(event:MouseEvent):void 
        { 
                Mouse.cursor = "arrow"; 
        } 
             
        private function createLines(textBlock:TextBlock):void 
        { 
            if(textBlock.firstLine) 
                removeChild (textBlock.firstLine); 
            var textLine:TextLine = textBlock.createTextLine (null, 300); 
            textLine.x = 15; 
            textLine.y = 20; 
            addChild (textLine); 
        } 
    } 
}

mouseOverHandler() mouseOutHandler() 函数在光标位于单词“here”上方时将光标设置为按钮光标,在光标不在“here”上方时将光标恢复为箭头。