2013-05-14 88 views
1

我需要在textArea中獲取插入符號的XY位置。在TextArea中獲取插入符號的XY位置

我已經找到一種方式來獲得的Y,但我不知道怎麼去X,誰能幫助?

如果ANY1想知道,這是我如何得到Y:

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

你有沒有嘗試之前和/或插入符號之後的字符getCharBoundaries()? http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html#getCharBoundaries() – Kodiak 2013-05-14 12:07:56

+0

的問題是,我需要從行的文本框年底獲得子(不只是當我按ENTER鍵)我的結束插入符號,但這是有點問題,因爲它沒有標記\ r – 2013-05-14 12:32:30

+1

http://stackoverflow.com/questions/12627185/get-caret-position-xy-in-input-文本框-AS3 – RafH 2013-05-14 21:28:59

回答

0

從RafH檢查鏈接後,我這樣做:

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; 
} 

它不是完美的,但它不夠好我

相關問題