Search code examples
flashactionscript-3actionscriptflash-cs4

How can I vertically center a Multiline Textfield (dynamic, text-centered) to another MovieClip?


Normally, to center an instance on the stage to another vertically horizontally, you can use simple logic and by referencing width, height, x and y co-ordinates of the symbols involved.

This seems impossible in-situ, because Textfield.height and Textfield.textHeight report incorrect values. They bring back the same values, which don't reflect the actual height of the instance on the stage.

enter image description here

I have crafted this simple example to illustrate the problem: http://omnom.replete.nu/fails.fla

Thanks.


Solution

  • The height of the TextField does not relate to the Height of the text itself.
    Text height can change for a number of reasons. A good example would be the font family and font sizes.
    You need to use text metrics to get that info.
    ALL THE INFO HERE

    [EDIT]
    per your comments the last line of your scaleTextToFitInTextField function should do this. you need to find the center of both objects and subtract them then add the offset

    txt.x = cont.width/2 - txt.width/2 + cont.x
    txt.y = cont.height/2 - txt.height/2 + cont.y
    

    [EDIT EDIT]
    Also remove these 2 lines as they are now redundant

    red.tf.x = red.mchammer.x;
    red.tf.y = red.mchammer.y;