views:

546

answers:

3

I've ran into a weird problem with getCharBoundaries, I could not figure out what coordinate space the coordinates returned from the function was in. What ever I tried I could not get it to match up with what I expected. So I made a new project and and added simple code to highlight the last charater in a textfield, and all of a sudden it worked fine. I then tried to copy over the TextField that had been causing me problems, into the new project. And now the same weird offset appeared 50px on the x axis. Everything else was spot on. So after some headscracthing comparing the two TextFields, I simply can not see a difference in their properties or transformation.

So I was hoping that someone might now what property might affect the coordinates returned by getCharBoundaries.

I am using Flash CS4.

A: 

Works For Me(tm) (Flex Builder AS3 project):

[Embed(systemFont="Segoe UI", fontWeight="bold", fontName="emb",
 mimeType="application/x-font")]
private var EmbeddedFont:Class;

public function ScratchAs3()
{
 stage.scaleMode = 'noScale';
 stage.align = 'tl';

 var m:Matrix = new Matrix(.8, .1, -.1, 1.1, 26, 78);

 var t:TextField = new TextField();
 t.autoSize = 'left';
 t.wordWrap = false;
 t.embedFonts = true;
 t.defaultTextFormat = new TextFormat("emb", 100, 0, true);
 t.transform.matrix = m;
 t.text = "TEST STRING.";
 addChild(t);

 var r:Rectangle = t.getCharBoundaries(8);
 var tl:Point = m.transformPoint(r.topLeft);
 var tr:Point = m.transformPoint(new Point(r.right, r.top));
 var bl:Point = m.transformPoint(new Point(r.left, r.bottom));
 var br:Point = m.transformPoint(r.bottomRight);
 graphics.beginFill(0xFF, .6);
 graphics.moveTo(tl.x, tl.y);
 graphics.lineTo(tr.x, tr.y);
 graphics.lineTo(br.x, br.y);
 graphics.lineTo(bl.x, bl.y);
 graphics.lineTo(tl.x, tl.y);
}

To literally answer your question, it returns the coordinates in the TextField's coordinate system, not it's parent, and it is affected by DisplayObject.transform.matrix, which is the backing for the .x, .y, .scaleX, .scaleY, .width, .height, and .rotation properties.

Simon Buchan
I got the thing working with a new TextField, I just want to know what can affect the x coordinate returned.I already checked scaling, and while that does effect the coordinates, it is not the cause here.
Lillemanden
+1  A: 

Well the getCharBoundaries returns the boundaries in the textfield coordinate system. Where the origin is topleft corner of the textfield.
getCharBoundaries does not take into consideration the scrolling. you need to check if there are scrollbars on its parent (textarea) and if so relocate. One quick way of doing it is using localtoglobal and globaltolocal. Use the first to translate from the textfield coordinate system to the application coordinate system and then use the second to translate from the app coordinate system to the coordinate system of the parent of the textfield which is the textarea. I'm fine tuning a my method to get char boundaries i will publish it today on my blog http://flexbuzz.blogspot.com/

Ahmad Hamid
Good suggestion, but it was not the problem. But like I wrote it was a property on the TextField. Never found out which though.
Lillemanden
A: 

What ever it was the solution was simple to add a new TextField, never found out what property screwed everything up.

Lillemanden