Search code examples
actionscript-3apache-flextextareaflash-builder

Get the XY position of the caret in TextArea


I need to get the XY position of the caret in textArea.

I already found a way to get the Y, but i dont know how to get X, can anyone help?

If any1 want to know, this is how i get Y:

var textBeforeCaret:String = text.substr(0, caretIndex);
var textDump:String = this.text;
this.text = textBeforeCaret;
this.validateNow();
yVariable = this.textHeight;
this.text = textDump;

Solution

  • After checking the link from RafH, i did this:

    var popUpX:int = 0;
    var popUpY:int = 0;
    if(text.length > 0){
    
    if(caretIndex > 0){
                                    if(this.textField.getCharBoundaries(caretIndex -1) != null){
    popUpX = this.textField.getCharBoundaries(caretIndex - 1).right;
    popUpY = this.textField.getCharBoundaries(caretIndex - 1).bottom;
    }
    else{
    popUpX = 0;
    var i:int = 2;
                                        while(this.textField.getCharBoundaries(caretIndex - i) == null && caretIndex - i > -1){
    i++;
    }
    if(caretIndex - i > -1){
    popUpY = this.textField.getCharBoundaries(caretIndex - i).bottom + i * 10;
    }
    else{
    popUpY = i * 10;
    }
    }
    }
    else{
    popUpX = this.textField.getCharBoundaries(caretIndex).right;
    popUpY = this.textField.getCharBoundaries(caretIndex).bottom;
    }
    }
    else{
    popUpX = 0;
    popUpY = 0;
    }
    

    it isnt perfect, but its good enough for me