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

Vertically center dynamic text

Contributor ,
Jan 08, 2018 Jan 08, 2018

Copy link to clipboard

Copied

Is it possible to have text in a dynamic textfield vertically centered? I wish to have the text vertically centered on a colored rectangle. Sometimes that text will have 1 line and sometimes 2. So I was wondering if there was a way to have that textfield vertically centered on that rectangle to no matter what that text will always be in the center of that shape?

Views

1.6K

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

LEGEND , Jan 09, 2018 Jan 09, 2018

CreateJS has a getMeasuredHeight() function. If you have a dynamic textfield named "tt", and a movieclip named "line" (that is just a thin stroke), this script would center the text on the line:

this.tt.text = "hello world";

//this.tt.text = "hello world how are you doing today isn't it nice weather";

var theight = this.tt.getMeasuredHeight();

this.tt.y = this.line.y - theight / 2;

Try uncommenting the second line to see the effect.

Votes

Translate

Translate
LEGEND ,
Jan 08, 2018 Jan 08, 2018

Copy link to clipboard

Copied

Specify AS3 or Canvas.

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
Contributor ,
Jan 09, 2018 Jan 09, 2018

Copy link to clipboard

Copied

In HTML5 Canvas

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
LEGEND ,
Jan 09, 2018 Jan 09, 2018

Copy link to clipboard

Copied

CreateJS has a getMeasuredHeight() function. If you have a dynamic textfield named "tt", and a movieclip named "line" (that is just a thin stroke), this script would center the text on the line:

this.tt.text = "hello world";

//this.tt.text = "hello world how are you doing today isn't it nice weather";

var theight = this.tt.getMeasuredHeight();

this.tt.y = this.line.y - theight / 2;

Try uncommenting the second line to see the effect.

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
New Here ,
Jan 05, 2021 Jan 05, 2021

Copy link to clipboard

Copied

LATEST

Appreciate your answer @Colin Holgate , I've been looking for a solution for two days now. 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