David
David

Reputation: 21

Flash vertical text alignment in middle

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!

Upvotes: 2

Views: 23939

Answers (6)

marius
marius

Reputation: 35

I had my text fields on the timeline in my situation and they were already sized accordingly to my needs (their size represented the align-to area) and set on Behaviour: Multiline.

But I guess the text fields can be created and sized from the ActionsScript as well.

So, I updated my text fields with the following function:

function fitText(field:TextField, myString:String):void {
    var initialHeight:Number = field.height;
    field.autoSize = "center";//doesn't really affect the alignment, it just makes the texfield autosizeable. 
    field.multiline = true;
    field.wordWrap = true;
    field.text = myString;
    field.y += (initialHeight - field.height) / 2;
}

Upvotes: 0

arnaud maignan
arnaud maignan

Reputation: 1

Give this a try,

function centralizeV(TT:TextField) {
    var TL:Number;
    if (TT.numLines > 1) TL = TT.textHeight / (TT.numLines - 1)
    else TL = TT.textHeight;
    var deltaL:int = int((TT.height - TT.textHeight) / 2 / TL);
    for (var k:int = 0; k < deltaL; k++) {
        TT.text = "\n" + TT.text;
    }
}

Upvotes: -1

Scorpion Inc
Scorpion Inc

Reputation: 31

Typically I use this code when I want to vertically align my text around the origin.

//Reposition Vertically
//for smaller/larger fonts
field._y = (-1 * field.textHeight) / 4;

Upvotes: 3

H&#233;ctor
H&#233;ctor

Reputation: 41

Only the TLFTextField (class in the package fl.text) has a built-in property to set the text vertical alignment.

Check http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/text/TLFTextField.html

Upvotes: 4

Joshua Maerten
Joshua Maerten

Reputation: 183

var my_text:TextField = new TextField();
addChild(my_text);

my_text.y = (stage.stageHeight/2)-(my_text.height/2);

Upvotes: -1

Antti
Antti

Reputation: 3159

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;

Upvotes: 8

Related Questions