开发者

getCharIndexAtPoint() equivalent in Spark RichEditableText

I want to find a way to get the character index in a Spark based RichEditableText bas开发者_StackOverflow社区ed on mouse x, y position.

The mx.controls.TextArea has a protected getCharIndexAtPoint() method but I can't find an equivalent of this in the Spark RichEditableText which is disappointing.

Any ideas or recommendations?


I can see why. seems RichEditableText uses FTE, while TextArea uses TextField, so you can just use TextField::getCharIndexAtPoint. you may just as well have no char at a point.

It's a long time since I've had a a look at FTE, but I think TextLine::getAtomIndexAtPoint would be a good start. Also, you should have a look at 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

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


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;
}


I had the same problem. The answer Even Mien gave did not work for me, initially.

With the below changes I got it working.

        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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜