View Full Version : TextField text co-ordinates
ndmccormack
06-02-2008, 09:34 AM
Hi there,
Is is possible to return the x and y positions of a specific bit of text in a textfield?
Cheers
Niall
lordofduct
06-02-2008, 09:48 AM
yes, but not as easy as you would probably hope.
Go to the livedocs for the TextField class. And read about all the methods and properties available to you. One thing you should be looking for is
TextLineMetrics
check out what methods that utilize this will get you to your result as easily as possible:
TextField livedocs: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextField.html
TextLineMetrics livedocs: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextLineMetrics.html
ignore all inherited props and methods. They are of no use to you as TextField inherits from InteractiveObject which has nothing to do with text itself.
amarghosh
06-02-2008, 11:07 AM
TextField::getCharBoundaries(index:int); (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextField.html#getCharBoundaries()) returns a rectangle that is bounding rectangle of character at index;
here is the code that i use to highlight the occurances of searchTextField.text in a large text field namely logTextField; highlight is a sprite added to parent of textfield at same x and y positions as the textfield;
may be it will help u;
private function onSearchText(e:Event = null):void
{
var key:String = searchTextField.text;
highlight.graphics.clear();
highlight.graphics.lineStyle(1, 0, 0.3);
if(key.length == 0)
return;
var log:String = logTextField.text;
var regEx:RegExp = new RegExp(key, "ig");
var result:Object = regEx.exec(log);
var index:Number;
var rect:Rectangle = null;
while(result != null)
{
index = result.index;
rect = logTextField.getCharBoundaries(index);
if(rect == null)
{
result = regEx.exec(log);
if(regEx.lastIndex != 0)
continue;
else
break;
}
for(var i:int = 1; i < (result[0] as String).length; i++)
{
var temp:Rectangle = logTextField.getCharBoundaries(index + i);
if(temp == null)
break;
rect.width += temp.width;
}
highlight.graphics.beginFill(0xffff00);
highlight.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
highlight.graphics.endFill();
result = regEx.exec(log);
if(regEx.lastIndex != 0)
continue;
else
break;
}
}
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.