| home |
A simple way to vertically align text within a text field in Flash. This is one of those things that's fairly easy to do, but I use it so infrequently that each time I have to sit and figure it out again. No more! hehehe.. here it is..
here's all the code that is in the SWF:
text_component.sample_txt.autoSize = true;
text_component.sample_txt.htmlText = "hello world <br />second <br />line";
text_component.sample_txt._y = (text_component.bGround._height * .5) - (text_component.sample_txt._height*.5);
basically there's a movieclip that contains the text field and a background. The text field needs to be vertically centered in relation to the background in the movieclip. So we just need to find the height of the text field and the height of the background and do the stuff.
here's a zip with the FLA, etc..
oh.. and here's a bonus.. how to restretch a text field if it's in a movieclip that has been stretched.. think of a button that you transform horizontally to make it wider. If the movieclip contains a text field then the text field gets stretched.. so we need it back to natural size.. so within the mc we have..
function setWidth(){
var w = this._width;
var diff = w-83; // assuming the native size of the mc containing the text field was 83
var stretch = w/83;
//rescale (since the parent clip has been scaled)
label_txt._xscale = (100*(1/stretch));
// now resize to actual size
label_txt._width = w;
}
and that will readjust the text field to match the new width of the movieclip