Vertical vertical alignment in the middle

I was looking for a way programmatically and by default set the dynamic textbox to vertically align in the middle of the box. It is very difficult for me to believe that there is no way to do this if I am not too blind. How else can I fake it?

Thank!

+2


source to share


6 answers


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;

      



+8


source


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



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

+4


source


I usually use this code when I want to vertically align text to the origin.

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

      

+3


source


I had textboxes in the timeline in my situation, and they were already adjusted according to my needs (their size represented the alignment region) and set to Behavior: Multiline.

But I think that text boxes can be created and sorted using ActionScript.

So, I updated my textboxes 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;
}

      

0


source


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

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

      

-1


source


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;
    }
}

      

-1


source







All Articles