我需要获取输入文本字段中插入符号的位置。我不需要设置位置,我需要获取插入符的当前位置。无法弄清楚这一点,这让我发疯!
检查文本字段.caretIndex财产和TextField.getCharBoundaries()文档中的方法。这实际上很简单,但当字段为空或下一个字符为空/换行符时,就会变得棘手。
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)');
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)