I want to know if it is possible to change some kind of event by listening to the cursor. I’m in I can’t find anything in the document.
Does anyone know what programs I can listen to here?
Thank you
function getPhraseLocation(phrase:String, field:TextField):Array {
// initialise the return array
var locations:Array = new Array();< br /> // find the first and last chars
var firstChar = field.text.indexOf(phrase);
var lastChar = firstChar+phrase.length;
// determine the bounding rectangle of the first char
var firstCharRect = field.getCharBoundaries(firstChar);
var crtLocation:Rectangle = new Rectangle(firstCharRect.left,firstCharRect.top,firstCharRect.width,firstCharRect.height);
// while there are chars left in the string
var crtChar:uint = firstChar;
while (++crtChar// if they're on the same line, expand the current rectangle
if (field.getCharBoundaries(crtChar ).y==crtLocation.y) crtLocation.width = uint(crtLocation.width)+field.getCharBoundaries(crtChar).width;
// if they're on the next line, due to word wrapping, create a new rectangle
else {
locations.push(crtLocation);
var crtCharRect = field.getCharBoundaries(crtChar);
crtLocation = new Rectangle(crtCharRect.left,crtCharRect.top ,crtCharRect.width,crtCharRect.height);
}
// add the last rectangle to the array
locations.push(crtLocation);
// return the array
return(locations);
}
We assume that we have created a TextField as follows:
var field:TextField = new TextField();
this.addChild(field);
// move the text field to some random coordinates
field.x = 50;
field.y = 50;< br />// set wordwrap to true, to test the multiline behaviour of our function
field.wordWrap = true;
// set a smaller width than our text
field.width = 300 ;
// disable selectability, I'm not sure it would work properly, anyway
field.selectable = false;
// fill the textfield with some random html text
field.htmlText = ' Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam et elementum lorem. Praesent vitae nunc at mi venenatis auctor.';
< p>Now, in order to have an event listener, we must create an object and draw rectangles on the actual text. The rectangles are drawn with 0% alpha, so they are not visible.
< pre>// create a sprite and add it to the display list
var overlay:Sprite = new Sprite();
this.addChild(overlay);
// enable mouse actions on it and make the cursor change on hover
overlay.mouseEnabled = true;
overlay.buttonMode = true;
// call the function that returns the size and position of the bounding boxes
var locationArray:Array = getPhraseLocation(‘elit. Aliquam et’,field);
// draw each rectangle in white transparent fill
for each (var bounds:Rectangle in locationArray) {
overlay .graphics.beginF ill(0xff0000,0);
overlay.graphics.drawRect(bounds.x+field.x-overlay.x, bounds.y+field.y-overlay.y, bounds.width, bounds.height);
overlay.graphics.endFill();
}
Then add an event listener for MouseOver:
overlay.addEventListener (MouseEvent.MOUSE_OVER, mouseOverHandler);
function mouseOverHandler(evt:MouseEvent):void {
trace('mouse over key phrase');
// do whatever else you want to do< br />}
Unfortunately, because we have drawn something on the actual text, the link becomes inactive. Therefore, we have to add an event listener for the click, also:
overlay.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(evt:MouseEvent):void {
navigateToURL(new URLRequest('http:// www.stackoverflow.com'));
}
Because we set the buttonMode property to true before, the mouse will change its cursor, and its behavior is exactly the same as when the link in the text is valid Same.
I defined a lot of variables to keep the code easier to understand. The code can be shortened and optimized, but it should also work.
For the simplest tasks, This is a workaround, but it does work. Hope it is useful.
I am trying to find out that a link is “hovering” in a text area where html text is displayed Time.
I want to know if it is possible to change some kind of event by listening to the cursor. I can’t find anything in the document.
Does anyone know what programs I can listen to here?
Thank you
This is a very interesting question. Using Cay’s suggestion, I thought of a method that will Returns an array of Rectangle objects corresponding to the position of the text. I am using plurals because if the text is a word hit, multiple rectangles may be required.
function getPhraseLocation(phrase:String, field:TextField):Array {
// initialise the return array
var locations:Array = new Array();
// find the first and last chars< br /> var firstChar = field.text.indexOf(phrase);
var lastChar = firstChar+phrase.length;
// determine the bounding rectangle of the first char
var firstCharRect = field .getCharBoundaries(firstChar);
var crtLocation:Rectangle = new Rectangle(firstCharRect.left,firstCharRect.top,firstCharRect.width,firstCharRect.height);
// while there are chars left in the string< br /> var crtChar:uint = firstChar;
while (++crtChar// if they're on the same line, expand the current rectangle
if (field.getCharBoundaries (crtChar).y==crtLocation.y) crtLocation.width = uint(crtLocati on.width)+field.getCharBoundaries(crtChar).width;
// if they're on the next line, due to word wrapping, create a new rectangle
else {
locations. push(crtLocation);
var crtCharRect = field.getCharBoundaries(crtChar);
crtLocation = new Rectangle(crtCharRect.left,crtCharRect.top,crtCharRect.width,crtCharRect.height);
}
// add the last rectangle to the array
locations.push(crtLocation);
// return the array
return(locations);
}
We assume that we have created a TextField as follows:
var field:TextField = new TextField();
this.addChild(field);
// move the text field to some random coordinates
field.x = 50;
field.y = 50;
// set wordwrap to true, to test the multiline behaviour of our function
field.wordWrap = true;
// set a smaller width than our text
field.width = 300;
// disable selectability, I'm not sure it would work pro perly, anyway
field.selectable = false;
// fill the textfield with some random html text
field.htmlText ='Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam et elementum lorem. Praesent vitae nunc at mi venenatis auctor.';
Now, in order to have an event listener, we must create An object and draw rectangles on the actual text. The rectangles are drawn with 0% alpha, so they are invisible.
// create a sprite and add it to the display list
var overlay:Sprite = new Sprite();
this.addChild(overlay);
// enable mouse actions on it and make the cursor change on hover
overlay.mouseEnabled = true;
overlay.buttonMode = true;
// call the function that returns the size and position of the bounding boxes
var locationArray:Array = getPhraseLocation('elit. Aliquam et', field);
// draw each rectangle in white transparent fill
for each (var bounds:Rectangle in locationArray) {
overlay.graphics.beginFill(0xff0000,0);
overlay.graphics.drawRect(bounds.x +field.x-overlay.x, bounds.y+field.y-overlay.y, bounds.width, bounds.height);
overlay.graphics.endFill();
}
Then add an event listener for MouseOver:
overlay.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
function mouseOverHandler(evt:MouseEvent): void {
trace('mouse over key phrase');
// do whatever else you want to do
}
Unfortunately, because we are in the actual text Something is drawn on the screen, so the link becomes inactive. Therefore, we must add an event listener to click, also:
overlay.addEventListener(MouseEvent.CLICK, clickHandler );
function clickHandler(evt:MouseEvent):void {
navigateToURL(new URLRequest('http://www.stackoverflow.com'));
}
Because we set the buttonMode property to true before, the mouse will change its cursor, and its behavior is exactly the same as when the link in the text is valid.
I defined a lot of variables to keep the code easier Understand. The code can be shortened and optimized, but it should also work.
For the simplest tasks, this is a workaround, but it does work. Hope it is useful.
>