Copy link to clipboard
Copied
Is there a way to position a TextField based on the vertical and horizontal center of its string? I gather the .myText.width and myText.height can be read, but the native origin seems to be in the upper left, as shown here:
so it will take trig todecide the offset to the center. Is there a native function to position at the actual center?
Very kind. That works. I had tried substracting half the width and height before, but it seems:
txt.autoSize = TextFieldAutoSize.CENTER;
makes the difference.
Thank you.
Copy link to clipboard
Copied
// AS3 code
var calculatedLocation:Object = {x:100, y:100}; // ***
var txt:TextField = new TextField();
txt.type = TextFieldType.DYNAMIC;
txt.autoSize = TextFieldAutoSize.CENTER;
txt.border = true; // ***
txt.text = "TEST"; // ***
txt.x = calculatedLocation.x - txt.width / 2;
txt.y = calculatedLocation.y - txt.height / 2;
addChild( txt );
Copy link to clipboard
Copied
Very kind. That works. I had tried substracting half the width and height before, but it seems:
txt.autoSize = TextFieldAutoSize.CENTER;
makes the difference.
Thank you.
Copy link to clipboard
Copied
Hi.
You can also use the textWidth and textHeight properties to get the actual size of the text. Then you can add or remove half of these values to position the TextField in the center.
Regards,
JC
Copy link to clipboard
Copied
That also seems to work since textWidth and textHeight are smaller and, I assume, more exact than width and height. Thanks.