查找文本组件(Flex/Actionscript)中鼠标悬停的单词

发布于 2024-07-18 17:02:14 字数 177 浏览 7 评论 0原文

是否有可能(如果可以的话如何)找出当光标/鼠标移动到 << 时,光标/鼠标位于哪个单词上? mx:文本> 成分? 因此,例如,当用户沿着句子(在文本组件内)移动鼠标时,每个单词都会随着移动而突出显示(我知道您可以在按下鼠标按钮时突出显示 - 但这不是我希望的方式)。

感谢您提供任何信息。

Is it possible (if so how) to find out what word the cursor/mouse is over when it is moving over a < mx:Text > component?
So for example as the user moves the mouse along a sentence (inside text component), each word will highlight as they go (I know you can highlight while pressing the mouse button down - but that not how I wish to do it).

Thanks for any info.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

护你周全 2024-07-25 17:02:14

一种方法是:您需要创建自己的组件来扩展 mx:Text 组件。 我在这个例子中使用了MyText。 下面是 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 对象的方法,查找鼠标坐标下的字符索引,然后查找该字符所属的单词。 这是一个简单的示例,您可能需要使其更加复杂以供实际使用。

Here's one way to do it: you need to create your own component that extends the mx:Text component. I used MyText in this example. Here's the full code for 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>

It works by accessing methods of the TextField object inside the Text component, finding the character index under the mouse coordinates and then finding the word the character belongs to. This is a quick example, you probably need to make it more elaborate for real world use.

谁把谁当真 2024-07-25 17:02:14

您需要使用 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);
}

不确定你想如何处理开始选择,但类似的东西应该可以工作。

You need to use the TextSnapshot class. You can grab it from your text control from the textSnapshot property. TextSnapshot has a hitTestTextNearPos() function that you can use to determine which character the user's mouse is near.

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

Not sure how you want to handle starting the selection, but something like that should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文