AS3: Getting the x & y value of a matched string in a text field
I have a text fie开发者_StackOverflow社区ld within Flash that contains a block of (obviously) text.
What I want to do is perform a search on the content of the text field that returns the x & y
coordiante and the width & height
of the found text. The result will be used to place a visual element over that portion of the text box.
For example:
var pattern:RegExp = /\d+/g;
var found:Array = box.text.match(pattern);
var i:String;
for each(i in found)
{
/**
* Find the x, y, width, height of the matched text in the text field
* Use result to place transparent yellow box around text
*/
}
Which visually should result in something like:
You would want to make use of the TextField class's getCharBoundaries
method, which accepts character indexes and returns a Rectangle object.
As far as I know, your match
method returns the strings themselves, so first you'll probably need to use the String class's indexOf
method, find all your character indexes, pass them to the getCharBoundaries
method, then draw some rectangles based on that output.
Here's some reference.
Good luck!
精彩评论