1

我想找到一种方法来根据鼠标 x、y 位置在基于 Spark 的 RichEditableText 中获取字符索引。

mx.controls.TextArea 有一个受保护的 getCharIndexAtPoint() 方法,但我在 Spark RichEditableText 中找不到与之等效的方法,这令人失望。

有什么想法或建议吗?

4

4 回答 4

1

我明白为什么了。似乎 RichEditableText 使用 FTE,而 TextArea 使用 TextField,所以您可以只使用TextField::getCharIndexAtPoint。你也可以在某一点上没有字符。

很久没看过 FTE,但我认为TextLine::getAtomIndexAtPoint将是一个好的开始。此外,您应该看看TLFTextField::getCharIndexAtPoint

于 2010-03-25T12:46:49.487 回答
1

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

http://www.justinpante.net/?p=201

于 2010-11-01T06:57:58.900 回答
0

我有同样的问题。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;
于 2012-04-29T22:33:32.380 回答
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;
}
于 2010-12-02T18:09:51.023 回答