Skip to main content
Inspiring
January 8, 2018
Answered

Vertically center dynamic text

  • January 8, 2018
  • 1 reply
  • 2265 views

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?

    This topic has been closed for replies.
    Correct answer Colin Holgate

    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.

    1 reply

    Legend
    January 8, 2018

    Specify AS3 or Canvas.

    jiggy1965Author
    Inspiring
    January 9, 2018

    In HTML5 Canvas

    Colin Holgate
    Colin HolgateCorrect answer
    Inspiring
    January 9, 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.