开发者

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 excessive开发者_JS百科ly blind. Else how can I fake it?

Thanks!


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;


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


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;


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


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

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜