package { import flash.display.StageScaleMode; import flash.display.StageAlign; import flash.display.BitmapData; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.text.StyleSheet; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; /** * TextField Rollover Test * * @mxmlc -debug * @author David Knape */ [SWF(backgroundColor="0xffffff",frameRate="31")] public class TextFieldRollover extends Sprite { private var contentTxt:TextField; private var statusTxt:TextField; private var snapshotBmp:BitmapData; private var _over:Boolean=false; public function TextFieldRollover() { addEventListener( Event.ADDED_TO_STAGE, init ); } private function init(e:Event) : void { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; // create text fields var tf:TextFormat=new TextFormat('Arial', 14); statusTxt = new TextField(); statusTxt.x = 40; statusTxt.y = 40; statusTxt.autoSize = TextFieldAutoSize.LEFT; statusTxt.defaultTextFormat = tf; contentTxt = new TextField(); contentTxt.x = 40; contentTxt.y = 60; contentTxt.autoSize = TextFieldAutoSize.LEFT; contentTxt.defaultTextFormat = tf; contentTxt.cacheAsBitmap = true; contentTxt.selectable = false; contentTxt.multiline = true; contentTxt.wordWrap = true; contentTxt.width = 200; contentTxt.htmlText = "

Hello, this is a test. It would be nice if we could detect the links that were rolled over.

"; // add to stage addChild(statusTxt); addChild(contentTxt); // add stylesheet to content text field so that there is a change on link rollovers var css:StyleSheet=new StyleSheet(); css.parseCSS(" a { color: #000088; } a:hover { text-decoration:underline; color: #008800; } "); contentTxt.styleSheet = css; snapshotBmp = new BitmapData(contentTxt.width, contentTxt.height, true); snapshotBmp.draw(contentTxt); contentTxt.addEventListener(MouseEvent.MOUSE_MOVE, checkIt); } private function checkIt(event:Event):void { // use bitmap data to check rollover var bmp:BitmapData=new BitmapData(contentTxt.width, contentTxt.height, true); bmp.draw(contentTxt); if(bmp.compare(snapshotBmp)) { if(!_over) { _over = true; // find the word var idx:int=contentTxt.getCharIndexAtPoint(contentTxt.mouseX, contentTxt.mouseY); var src:String=contentTxt.text; var s:String=""; var c:String; while(idx > 0 && src.charAt(idx) != " ") idx--; while(++idx < src.length && (c = src.charAt(idx)) != " " && c != ".") s += c; statusTxt.text = 'Rolled over "' + s + '"'; } } else { if(_over) { statusTxt.text = ''; _over = false; } } bmp.dispose(); } } }