Flex中,如何获取TextArea中的光标的位置

要获取光标的index很容易,其实Flash原本的flash.text.TextField就提供这个功能,但在Flex里却没有把它暴露出来。不理解Adobe是怎么想的。 
要使用原本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绝望了……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值