6 Replies Latest reply on Jan 6, 2011 7:51 AM by ozDiGennaro

    Detect mouse over for each word in Text

    ozDiGennaro Level 1
      For large text components, I must detect mouse events (over, out, click) separately for each word (or even for each letter) in a text component.
      Which of the text components should I use?
      Can I use mouseMove events? If so, how do I know where the words are?
      "getLineMetrics" is close, but pretty tedious, since I'd have to do a LOT of bookkeeping.
      Thanks.
        • 1. Re: Detect mouse over for each word in Text
          injpix Level 3
          I believe the best way would be to create a hyperlink and assign it to a textField. Such as:

          theTextField.htmlText = “<a href=’event:linkContent’>linkText</a>”;

          theTextField.addEventListener(TextEvent.LINK, linkListener);

          private function linkListener(e:TextEvent):void
          {
          }


          “The linkContent string typically identifies an ActionScript operation to be carried out when the link is clicked.”
          – Essential ActionScript 3.0 p.577
          • 2. Re: Detect mouse over for each word in Text
            ozDiGennaro Level 1
            Thanks for the reply. This wil provide one link for the whole textField. I want to avoid creating a textField for each word, since it's probably slow and I'll have to do the work that the text renderer usually does, including word spaces, line wrap, etc.
            I want to use a small number of textfields for many lines of text, and then detect which word the mouse moved over.
            Any ideas?
            • 3. Re: Detect mouse over for each word in Text
              rtalton Level 4
              I like myIP's idea. You assign a link to each word and add the whole thing to the HTMLtext property. Don't think you could make individual words or letters clickable without them being HTMLtext. This would result in a lot of html tags, though I see no other way. Plain text have no events associated with them.

              getLineMetrics returns a line of text, not individual letters or words.

              Consider giving us more details about what you are trying to accomplish by having the text 'clickable', and someone may have an idea for you.
              • 4. Re: Detect mouse over for each word in Text
                injpix Level 3
                "This wil provide one link for the whole textField"
                It could if you want it to be that way. It can also provide one link for each word or letter.

                Remember you can add multiple <a> tags to a textfield. Such as below:

                theTextField.htmlText = “<a href=’event:linkContent’>Adobe</a><a href=’event:linkContent’>Flex</a>”<a href=’event:linkContent’>Builder</a> 3";

                Does that help? "Adobe", "Flex" and "Builder" has their own links. The "3" does not have a link. Then you can have unique "linkContents" for each word, so that the listener will know what word or letter was clicked.

                I am not sure if this text will be loaded on a flat(static) or dynamic file. If its flat you could manually add each <a> tag where its needed. If its dynamic you would need to write a function or class to insert <a> tags where its needed.

                Let me know how it goes.
                • 5. Re: Detect mouse over for each word in Text
                  ozDiGennaro Level 1
                  This is dynamic text, or even text provided via a remote object.
                  And yes, I can make each word into a hypertext link. It's a bit clumsy and may have worse performance.
                  This application is text-heavy, so it matters.

                  Yes, "line metrics" describes the bounding box of a line of text, so it's only part of the solution, if there is one.

                  I'm investigating all of the properties and methods of the TextArea component. The Text component won't do it.

                  What I really want to do is catch "mouse move" events, and then "look up" in the underlying text object, what word or is under the cursor.

                  I'll report my results.
                  • 6. Re: Detect mouse over for each word in Text
                    ozDiGennaro Level 1

                    I have since moved on to using Text Layout Framework, which does everything that I need.

                    More about this soon.

                    Oz