Я написал этот класс Helper, который решает вашу проблему (если вы используете Flex 4):
</p>
<pre><code>public class TextAreaHelper
{
public static function getCaretPosition(ta : TextArea) : Point
{
var flowComposer : StandardFlowComposer = StandardFlowComposer(ta.textFlow.flowComposer);
var absoluteIndex : int = ta.selectionActivePosition;
var lineIdx : int = flowComposer.findLineIndexAtPosition(absoluteIndex);
if (lineIdx == flowComposer.numLines)
lineIdx--;
var prevLine : TextFlowLine = lineIdx != 0 ? flowComposer.getLineAt(lineIdx - 1) : null;
var nextLine : TextFlowLine = lineIdx != flowComposer.numLines-1 ? flowComposer.getLineAt(lineIdx + 1) : null
var result : Rectangle = flowComposer.getLineAt(lineIdx).computePointSelectionRectangle(absoluteIndex, ta, prevLine, nextLine, true);
if (result)
{
var point : Point = new Point(result.x, result.y);
point.x -= ta.scroller.viewport.horizontalScrollPosition;
point.y -= ta.scroller.viewport.verticalScrollPosition;
return point;
}
return null;
}
}
Вот пример использования:
</p>
<pre><code>:<fx:Script>
<![CDATA[
[Bindable]
private var caretPosition : Point;
private function textareaSelectionChangeHandler(e : FlexEvent) : void
{
updateCaretPosition();
}
private function onPossibleScrollPositionChange(e : PropertyChangeEvent) : void
{
if (["verticalScrollPosition", "horizontalScrollPosition"].indexOf(e.property) != -1)
{
updateCaretPosition();
}
}
private function updateCaretPosition() : void
{
caretPosition = TextAreaHelper.getCaretPosition(ta);
if (caretPosition) caretPosition = ta.localToGlobal(caretPosition);
trace(ObjectUtil.toString(caretPosition));
}
private function initTextArea() : void
{
ta.addEventListener(FlexEvent.SELECTION_CHANGE, textareaSelectionChangeHandler);
ta.textDisplay.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onPossibleScrollPositionChange);
}
]]>
</fx:Script>
<s:TextArea x="200" y="300" id="ta" creationComplete="initTextArea()" />
<s:Rect x="{caretPosition.x}" y="{caretPosition.y}" width="10" height="10" visible="{caretPosition != null}">
<s:fill>
<s:SolidColor color="yellow" />
</s:fill>
</s:Rect>