Domanda

È possibile (se sì, come) scoprire su quale parola si trova il cursore / mouse quando si sposta su un < mx: testo > componente? Ad esempio, quando l'utente sposta il mouse lungo una frase (all'interno del componente di testo), ogni parola verrà evidenziata man mano che procede (so che puoi evidenziare mentre premi il pulsante del mouse verso il basso, ma non è così che desidero farlo).

Grazie per qualsiasi informazione.

È stato utile?

Soluzione

Ecco un modo per farlo: devi creare il tuo componente che estende il componente mx: Text. Ho usato MyText in questo esempio. Ecco il codice completo per <=>:

<?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>

Funziona accedendo ai metodi dell'oggetto TextField all'interno del componente Testo, trovando l'indice dei caratteri sotto le coordinate del mouse e quindi trovando la parola a cui appartiene il carattere. Questo è un rapido esempio, probabilmente dovrai renderlo più elaborato per l'uso nel mondo reale.

Altri suggerimenti

Devi usare la classe TextSnapshot. Puoi prenderlo dal controllo del testo dalla proprietà textSnapshot. TextSnapshot ha una funzione hitTestTextNearPos () che puoi usare per determinare a quale personaggio si trova il mouse dell'utente.

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

Non sono sicuro di come gestire l'avvio della selezione, ma qualcosa del genere dovrebbe funzionare.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top