マウスがテキストコンポーネント(Flex / Actionscript)の上にある単語を探します

StackOverflow https://stackoverflow.com/questions/811595

質問

<!> ltの上を移動しているときに、カーソル/マウスがどの単語の上にあるかを見つけることは可能ですか? mx:Text <!> gt;成分? たとえば、ユーザーがテキスト(テキストコンポーネント内)に沿ってマウスを動かすと、各単語が強調表示されます(マウスボタンを押しながら強調表示できることはわかっていますが、それは私が望んでいる方法ではありません)。 / p>

情報をお寄せいただきありがとうございます。

役に立ちましたか?

解決

これを行う1つの方法は、mx:Textコンポーネントを拡張する独自のコンポーネントを作成する必要があることです。この例ではMyTextを使用しました。 <=>の完全なコードは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<mx:Text xmlns:mx="http://www.adobe.com/2006/mxml" mouseMove="onMouseMove(event)" initialize="init()">
    <mx:Script>
        <![CDATA[

            // Text formats
            private var normalTextFormat:TextFormat;
            private var highlightTextFormat:TextFormat;

            // Saved word start and end indexes
            private var wordStartIndex:int = -1;
            private var wordEndIndex:int = -1;

            private function init():void
            {
                normalTextFormat = textField.getTextFormat();
                normalTextFormat.color = 0;
                highlightTextFormat = textField.getTextFormat();
                highlightTextFormat.color = 0xFF0000;
            }

            private function onMouseMove(event:MouseEvent):void
            {
                // Clear previous word highlight
                textField.setTextFormat(normalTextFormat, wordStartIndex, wordEndIndex);

                var charIndexUnderMouse:int = textField.getCharIndexAtPoint(event.localX, event.localY);
                wordStartIndex = charIndexUnderMouse;
                wordEndIndex = charIndexUnderMouse;

                // Find start of word
                while (text.charAt(wordStartIndex) != " " && wordStartIndex > 0)
                {
                    wordStartIndex--;
                }

                // Find end of word
                while (text.charAt(wordEndIndex) != " " && wordEndIndex < text.length)
                {
                    wordEndIndex++;
                }

                // Highlight character
                textField.setTextFormat(highlightTextFormat, wordStartIndex, wordEndIndex);
            }
        ]]>
    </mx:Script>

</mx:Text>

Textコンポーネント内のTextFieldオブジェクトのメソッドにアクセスし、マウス座標の下で文字インデックスを見つけてから、文字が属する単語を見つけることで機能します。これは簡単な例です。実世界で使用するには、おそらくより複雑にする必要があります。

他のヒント

TextSnapshotクラスを使用する必要があります。 textSnapshotプロパティからテキストコントロールから取得できます。 TextSnapshotにはhitTestTextNearPos()関数があり、これを使用してユーザーのマウスがどの文字に近いかを判断できます。

...
var startIndex:Number;
...

private function textMouseMoveHandler(event:MouseEvent):void
{
    var snapshot:TextSnapshot = text.textSnapshot;
    var index = snapshot.hitTestTextNearPos(event.x, event.y);

    snapshot.setSelected(startIndex, index, true);
}

// I do not know how you want to begin the selection, so I put it here in the MouseEnter event.
private function textMouseEnterHandler(event:MouseEvent):void
{

    var snapshot:TextSnapshot = text.textSnapshot;
    startIndex = snapshot.hitTestTextNearPos(event.x, event.y);
}

選択の開始をどのように処理するかはわかりませんが、そのようなものが機能するはずです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top