Get caret position (x, y) in input TextField AS3?

前端 未结 2 1949
傲寒
傲寒 2021-01-24 21:29

I need to get the position of the caret in my input textfield. I don\'t need to set the position, i need to get the current location of the caret. Can\'t figure this out and it\

相关标签:
2条回答
  • 2021-01-24 22:10

    Check the Texfield.caretIndex property and TextField.getCharBoundaries() method in documentation. It's actually easy but it gets tricky when the field is empty or the next char is a empty/newline.

    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFieldType;
    import flash.events.KeyboardEvent;
    import flash.geom.Rectangle;
    
    var t:TextField = new TextField();
    t.x = t.y = 20;
    t.width = 200;
    t.height = 100;
    t.multiline = t.wordWrap = t.border = true;
    t.defaultTextFormat = new TextFormat('_sans',20);
    t.type = TextFieldType.INPUT;
    addChild(t);
    stage.focus = t;
    t.addEventListener(KeyboardEvent.KEY_DOWN,kd);
    
    function kd(e:KeyboardEvent):void {
        if (!e.ctrlKey) return;
        var car:int = t.caretIndex;
        var rect:Rectangle = t.getCharBoundaries(car);
        if (rect == null) {
            trace('caret:'+car,'need to calculate this possibility')
        } else {
            trace('caret:'+car,rect,'(relative to textfield)');
        }
    }
    
    0 讨论(0)
  • 2021-01-24 22:13

    You can get some information if you're using monospace font, like the one that's used in answer box. To get the caret position, there is caretIndex property to read it. You can use text property to number out newlines in the textfield, so you can get two coordinates in (row, column) system out of it. Then, use scrollH and scrollV to determine how far does the textfield's contents scrolled, and subtract them from the coordinates, then multiply them by one symbol's width and height. But, all of this wouldn't work if your textfield uses TrueType fonts of variable symbol width. The best you could get is current height, as the height if one line of text is constant throughout the textfield.

    Edit: With the text field's wordWrap property being true, the correct height depends on that textfield's width. You may try the following:

            var storeText:String = tf.text.slice();
            var lineHeight:Number;
            if (tf.numLines == 0) lineHeight = 1; 
                            else lineHeight = tf.textHeight / tf.numLines;
            tf.text=storeText.slice(0, tf.caretIndex);
            var m:int = tf.numLines-tf.scrollV;
            trace(m*lineHeight);
            tf.text = storeText;
    

    The method is rather stupid, and involves changing text property of a textfield two times, which is slow. This returns the number that is the offset of the current line from the top of the textfield. I expect this is what you need to know. If you want to know the relative position from the top of the unscrolled text, ignore tf.scrollV.

    0 讨论(0)
提交回复
热议问题