Encuentre la palabra que el mouse ha terminado para el componente de texto (Flex / Actionscript)
-
03-07-2019 - |
Pregunta
¿Es posible (si es así, cómo) averiguar qué palabra tiene el cursor / mouse cuando se mueve sobre un < mx: Texto > ¿componente? Entonces, por ejemplo, a medida que el usuario mueve el mouse a lo largo de una oración (dentro del componente de texto), cada palabra se resaltará a medida que avanza (sé que puede resaltar mientras presiona el botón del mouse hacia abajo, pero no es así como deseo hacerlo).
Gracias por cualquier información.
Solución
Aquí hay una forma de hacerlo: necesita crear su propio componente que extienda el componente mx: Text. Usé MyText
en este ejemplo. Aquí está el código completo para <=>:
<?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>
Funciona accediendo a métodos del objeto TextField dentro del componente Text, buscando el índice de caracteres bajo las coordenadas del mouse y luego buscando la palabra a la que pertenece el carácter. Este es un ejemplo rápido, probablemente necesite hacerlo más elaborado para el uso en el mundo real.
Otros consejos
Necesita usar la clase TextSnapshot. Puede tomarlo de su control de texto desde la propiedad textSnapshot. TextSnapshot tiene una función hitTestTextNearPos () que puede usar para determinar qué carácter está cerca del mouse del usuario.
...
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);
}
No estoy seguro de cómo quiere manejar el inicio de la selección, pero algo así debería funcionar.