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" 위에 있을 때는 버튼 커서로 변경되고, 그렇지 않을 때는 다시 화살표로 돌아가도록 설정합니다.