This is a very interesting problem. Using the Cay clause, I thought of a method that returns an object Arrayfrom Rectangleobjects matching text locations. I use the plural because several rectangles may be needed if the text is a word tapping.
function getPhraseLocation(phrase:String, field:TextField):Array {
var locations:Array = new Array();
var firstChar = field.text.indexOf(phrase);
var lastChar = firstChar+phrase.length;
var firstCharRect = field.getCharBoundaries(firstChar);
var crtLocation:Rectangle = new Rectangle(firstCharRect.left,firstCharRect.top,firstCharRect.width,firstCharRect.height);
var crtChar:uint = firstChar;
while (++crtChar<lastChar)
if (field.getCharBoundaries(crtChar).y==crtLocation.y) crtLocation.width = uint(crtLocation.width)+field.getCharBoundaries(crtChar).width;
else {
locations.push(crtLocation);
var crtCharRect = field.getCharBoundaries(crtChar);
crtLocation = new Rectangle(crtCharRect.left,crtCharRect.top,crtCharRect.width,crtCharRect.height);
}
locations.push(crtLocation);
return(locations);
}
, TextField :
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 properly, anyway
field.selectable = false;
// fill the textfield with some random html text
field.htmlText = 'Lorem ipsum dolor sit amet, consectetur adipiscing <a href="http://www.stackoverflow.com">elit. Aliquam et</a> elementum lorem. Praesent vitae nunc at mi venenatis auctor.';
, , . 0% , .
var overlay:Sprite = new Sprite();
this.addChild(overlay);
overlay.mouseEnabled = true;
overlay.buttonMode = true;
var locationArray:Array = getPhraseLocation('elit. Aliquam et',field);
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();
}
MouseOver:
overlay.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
function mouseOverHandler(evt:MouseEvent):void {
trace('mouse over key phrase');
}
, - , . , , :
overlay.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(evt:MouseEvent):void {
navigateToURL(new URLRequest('http://www.stackoverflow.com'));
}
buttonMode true, , , , .
, . , , .
, . , .