• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Position the center of a TextField

Participant ,
Apr 21, 2023 Apr 21, 2023

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:

pieMidpoints.jpg

so it will take trig todecide the offset to the center. Is there a native function to position at the actual center?

Views

416

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Apr 22, 2023 Apr 22, 2023

Very kind. That works. I had tried substracting half the width and height before, but it seems:

txt.autoSize = TextFieldAutoSize.CENTER;

makes the difference.

pieMidpointsAfter.jpg

Thank you.

Votes

Translate

Translate
Engaged ,
Apr 22, 2023 Apr 22, 2023

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 );

VladinMMitov_0-1682156372991.png

 

- Vlad: UX and graphic design, Flash user since 1998
Member of Flanimate Power Tools team - extensions for character animation

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 22, 2023 Apr 22, 2023

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.

pieMidpointsAfter.jpg

Thank you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 22, 2023 Apr 22, 2023

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 22, 2023 Apr 22, 2023

Copy link to clipboard

Copied

LATEST

That also seems to work since textWidth and textHeight are smaller and, I assume, more exact than width and height. Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines