Manipulação de eventos no FTE

Flash Player 10 e posterior, Adobe AIR 1.5 e posterior

É possível adicionar ouvintes de eventos a uma instância do TextLine assim como a outros objetos de exibição. Por exemplo, é possível detectar quando um usuário passa o mouse sobre uma linha de texto ou um usuário clica na linha. O exemplo a seguir detecta ambos os eventos. Quando o usuário passar o mouse sobre a linha, o cursor mudará para um cursor de botão, e quando ele clicar na linha, a cor do cursor mudará.
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); 
        } 
    } 
}

Espelhamento de eventos

Também é possível espelhar os eventos em um bloco de texto ou em uma parte de um bloco de texto para um dispatcher de eventos. Em primeiro lugar, crie uma instância do EventDispatcher e a atribua à propriedade eventMirror de uma instância do TextElement. Se o bloco for composto de um único elemento de texto, o mecanismo espelhará eventos para todo o bloco de texto. Se o texto for composto de vários elementos de texto, o mecanismo espelhará eventos apenas para as instâncias do TextElement que tenham a propriedade eventMirror definida. O texto no exemplo a seguir é composto por três elementos: a palavra "Click" (Clique), a palavra "here" (aqui) e a sequência de caracteres "to see me in italic" (para me ver em itálico). O exemplo designa um dispatcher de eventos ao segundo elemento de texto, a palavra "here" e adiciona um ouvinte de eventos, o método clickHandler() . O método clickHandler() altera o texto para itálico. Ele substitui também o conteúdo do terceiro elemento para rezar "Click here to see me in normal font!" (Clique aqui para me ver em fonte normal!)

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); 
        } 
    } 
}

As funções mouseOverHandler() e mouseOutHandler() muda o cursor para um cursor de botão quando ele está passando sobre a palavra "here" e o retorna para uma seta quando não está.