要使用原本Flash里就支持的这个功能很简单。mx.controls.TextArea里负责渲染的实际上是一个mx.core.UITextField,继承自mx.core.FlexTextField;后者又继承自flash.text.TextField。只要得到TextArea里的TextField就能使用它原本就支持的功能。
可是Flex的TextArea里textField是protected的,从外部无法获取,除非继承TextArea;TextArea.getTextField()方法是在mx_internal命名空间里的,不使用这个命名空间也无法调用。
于是我采用的办法是导入并使用mx.core.mx_internal命名空间,然后调用getTextField()方法来得到TextField。
导入和使用命名空间的代码:
import mx.core.mx_internal;
use namespace mx_internal;
那么获取当前光标index的方法就是:
public function getCaretIndex(ta : TextArea) : int {
return (ta.getTextField() as TextField).caretIndex;
}
如果是htmlText,index可能与预期的不同。参考这里的解决办法:http://www.flexer.info/2008/03/26/find-cursor-position-in-a-htmltext-object-richtexteditor-textarea-textfield-update/
获取光标的坐标则很麻烦。首先Flash的TextField本身似乎不直接支持这个操作,只支持它的逆操作:从坐标点获取字符。
还好TextField支持通过index来得到字符的边界(bounds),于是我们可以把光标右边的字符(也就是text.charAt(caretIndex))的左上角坐标当作是光标的坐标。
问题是光标位置所对应的index很可能是字符串的末尾,那么它的右边的字符是空串('');又或者光标右边的字符是换行符(Flex的TextArea里按回车似乎只会得到\r……)。这两种情况都会导致TextField.getCharBoundaries()的调用返回null,让事情变得很麻烦。
所以只能用很hacky的办法来算出光标的坐标了。TextField在渲染文字的时候,会在左边和上面各留出2像素的空位,所以下面的代码里可以看到有个magic number是2。这个数字是在TextLineMetrics类的文档里看到的。
public function getCaretLocation(ta : TextArea) : Point {
var tf : TextField = ta.getTextField() as TextField;
var index : Number = tf.caretIndex;
var x : Number, y : Number;
var rect : Rectangle;
if ('' == ta.text.charAt(index)) {
if (0 == index) {
// the TextArea is empty
x = 2;
y = 2;
} else if ('\n' == ta.text.charAt(index - 1) || '\r' == ta.text.charAt(index - 1)) {
// at the start of a new line, and also at the end of a the line
// use lineHeight * lineIndex + yPadding as y
x = 2;
y = tf.getLineMetrics(0).height * (tf.getLineIndexOfChar(index - 1) + 1) + 2;
} else {
// at the end of a line
// use the bounds of the charater to the left of caret, and add the width of it to x
rect = tf.getCharBoundaries(index - 1);
x = rect.x + rect.width;
y = rect.y;
}
} else {
if ('\n' == ta.text.charAt(index) || '\r' == ta.text.charAt(index)) {
// at the start of a line, but not at the end of the line
x = 2;
y = tf.getLineMetrics(0).height * tf.getLineIndexOfChar(index) + 2;
} else {
// in middle of a line
rect = tf.getCharBoundaries(index);
x = rect.x;
y = rect.y;
}
}
return new Point(x, y);
}
希望上面的代码有用吧。写这玩儿快让我对Adobe绝望了……