views:

1472

answers:

1

Hi there,

I've been looking for a way to programmatically and by default set a dynamic text box to vertically align in the middle of the box. I find it really hard to believe that there's no option to do this, unless I'm excessively blind. Else how can I fake it?

Thanks!

+1  A: 
var parentContainer:DisplayObjectContainer = ...;
var textField:TextField = ...;
textField.autoSize = TextFieldAutoSize.CENTER; 
// set width, height, wordWrap etc if needed

//after setting the text or in the textInput event handler if the 
//textField is user editable
textField.y = parentContainer.height * 0.5 - textField.textHeight * 0.5;
Antti
parent should be typed as `DisplayObjectContainer`
Amarghosh
`textFeild.autoSize` takes a String as its value (one of these ["center", "left", "right", "none"] - declared as TextFieldAutoSize.CENTER etc).
Amarghosh
`autoSize` is for left/right/center alignments. http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextField.html#autoSize
Amarghosh
The code will work with autoSize set to "center"; Move the last line to textField's TextEvent.TEXT_INPUT handler in case the text field is user editable. Edit the post to fix it and I'll remove the down vote.
Amarghosh
Edited the code and removed the down vote.
Amarghosh