Vertical-alignment for textfields in ActionScript 3.0 is useful for aligning textfields to images, other UI elements in a flash app, and more! As many of you may know, Adobe introduced TLF Text along with Flash Player 10. If you are working with general consumer projects, however, chances are you are targeting Flash Player 9 as a minimum.
Before TLF TextFields, there was no direct way to vertically align TextFields.
TextLineMetrics allows us to access basic values for any typeface, such as ascent, descent, leading, width and height. Using
ascent, we can determine a TextField’s baseline Y position.
Let’s assume that our TextField
myText is already on the stage. This is how you would get it’s baseline Y-axis:
var baselineY:Number = myText.y + myText.getLineMetrics(0).ascent;
Now, to align another TextField
myOtherText to this
myText, you would do this:
myOtherText.y = baselineY – myOtherText.getLineMetrics(0).ascent;
myOtherText.y = myText.y – myOtherText.getLineMetrics(0).ascent;
The second method can be used if you have 1 item that is stationary. Otherwise, store the fixed Y position in a variable, so you can move
myText around and move it back in line later.