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.