caretIndex, selectionBeginIndex, selectionEndIndex

by 9re
♥0 | Line 39 | Modified 2010-02-02 18:52:08 | MIT License
play

ActionScript3 source code

/**
 * Copyright 9re ( http://wonderfl.net/user/9re )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/amuI
 */

package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFieldType;

    public class TextField_caretIndex extends Sprite {
    		private var outputField:TextField;
    	
        public function TextField_caretIndex() {
            addChild(outputField = createCustomTextField(10, 120, 100, 100));
            
            var tf:TextField = createCustomTextField(10, 10, 100, 100);
            tf.wordWrap = true;
            //tf.type = TextFieldType.INPUT;
            tf.text = "Click in this text field. Compare the difference between clicking without selecting versus clicking and selecting text.";
            stage.focus = tf;
            tf.setSelection(10, 10);
            tf.addEventListener(MouseEvent.CLICK, printCursorPosition);
            tf.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
        }
        
        public function trace(...arguments:Array):void {
        		outputField.appendText(arguments + "\n");
        		outputField.scrollV = outputField.maxScrollV;
        		outputField.width = outputField.textWidth + 4;
        }

        private function printCursorPosition(event:MouseEvent):void {
            var tf:TextField = TextField(event.target);
            trace("caretIndex:", tf.caretIndex);
            trace("selectionBeginIndex:", tf.selectionBeginIndex);
            trace("selectionEndIndex:", tf.selectionEndIndex);
        }

        private function createCustomTextField(x:Number, y:Number, width:Number, height:Number):TextField {
            var result:TextField = new TextField();
            result.x = x;
            result.y = y;
            result.width = width;
            result.height = height;
            addChild(result);
            return result;
        }
    }
}

Forked