我想找到一种方法来根据鼠标 x、y 位置在基于 Spark 的 RichEditableText 中获取字符索引。
mx.controls.TextArea 有一个受保护的 getCharIndexAtPoint() 方法,但我在 Spark RichEditableText 中找不到与之等效的方法,这令人失望。
有什么想法或建议吗?
我想找到一种方法来根据鼠标 x、y 位置在基于 Spark 的 RichEditableText 中获取字符索引。
mx.controls.TextArea 有一个受保护的 getCharIndexAtPoint() 方法,但我在 Spark RichEditableText 中找不到与之等效的方法,这令人失望。
有什么想法或建议吗?
我明白为什么了。似乎 RichEditableText 使用 FTE,而 TextArea 使用 TextField,所以您可以只使用TextField::getCharIndexAtPoint。你也可以在某一点上没有字符。
很久没看过 FTE,但我认为TextLine::getAtomIndexAtPoint将是一个好的开始。此外,您应该看看TLFTextField::getCharIndexAtPoint。
I was looking for a similar solution after the heads up from back2dos i came up with the following solution, probably needs a bit of work but it functions
我有同样的问题。Even Mien 给出的答案最初对我不起作用。
通过以下更改,我得到了它的工作。
var globalPoint:Point = new Point(stage.mouseX, stage.mouseY);
var flowComposer:IFlowComposer = this.textFlow.flowComposer;
for (var i:int = 0; i < flowComposer.numLines; i++)
{
var textFlowLine:TextFlowLine = flowComposer.getLineAt(i);
var textLine:TextLine = textFlowLine.getTextLine(true);
var textRect:Rectangle = textLine.getRect(stage);
if (globalPoint.y >= textRect.top && globalPoint.y < textRect.bottom)
{
return textFlowLine.absoluteStart + textLine.getAtomIndexAtPoint(globalPoint.x, globalPoint.y);
}
}
return 0;
Here's what I used:
private function getCharAtPoint(ta:RichEditableText, x:Number, y:Number) : int
{
var globalPoint:Point = ta.localToGlobal(new Point(x, y));
var flowComposer:IFlowComposer = ta.textFlow.flowComposer;
for (var i:int = 0; i < flowComposer.numLines; i++){
var textFlowLine:TextFlowLine = flowComposer.getLineAt(i);
if (y >= textFlowLine.y && y < textFlowLine.height + textFlowLine.y)
{
return textFlowLine.absoluteStart
+ textFlowLine.getTextLine(true)
.getAtomIndexAtPoint(globalPoint.x, globalPoint.y);
}
}
return -1;
}