我用Google搜索了近一天的时间来找到我的问题的答案,并在polygeek找到了答案:
http://polygeek.com/1826_flex_monitoring-the-position-of-the-cursor-in-a-textarea
这几乎可行(并且是一个很好的解决方案),但是当我这样做时解决方案不起作用: 1)。复制并粘贴多行文本 2)。写长并滚动文本。
我还发现了另一个工作示例: http://www.victordramba.com/?p=31 (当您在插入符号的位置按ctrl +空格时,会出现自动完成提示) 但是其作者的电子邮件(Victor Dramba)说他使用单色字体来计算个人角色的位置,并且没有多大帮助。
有没有办法从文本字段中获取多行输入?
答案 0 :(得分:3)
我写过这个Helper类,它可以解决你的问题(如果你使用Flex 4):
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;
}
}
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;
}
}
以下是一个使用示例:
:<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>
答案 1 :(得分:1)
这是单一方式,标签字符标记为一个字符......
public class TextAreaEx extends TextArea
{
...
public function getCaretPosition() : Point
{
var pos : int = selectionActivePosition;
var tx:String = text;
var p:Point = new Point(pos+1, 1);
for(var i:int=pos-1; i>0; i--)
{
if (tx.charAt(i) == '\n')
{
if (p.y==1)
p.x = pos-i;
p.y++;
}
}
return p;
}
}