How to vertical align a TextField in AS3

前端 未结 4 977
轻奢々
轻奢々 2020-12-16 00:35

I´m trying to vertical align some text in a Monoline TextField in AS3. Not sure if this can be done with TextFormat, but I don´t think so.

I´ve looked for a solution

相关标签:
4条回答
  • 2020-12-16 01:03

    You can't automatically vertical align text. You need to make it by your own.

    Here's my working code:

    public static function verticalAlignTextField(tf: TextField): void {
        tf.y += Math.round((tf.height - tf.textHeight) / 2);
    }
    
    0 讨论(0)
  • 2020-12-16 01:03

    If you are able to use a fl.text.TLFTextField instead of a flash.text.TextField (available from Flash Player 10+), you can vertically center the text using the verticalAlign property:

    import flashx.textLayout.formats.VerticalAlign;
    [...]
    myTextField.verticalAlign = VerticalAlign.MIDDLE;
    
    0 讨论(0)
  • 2020-12-16 01:13

    i can assure that "walkietokyo" answer is the true answer but it should be used in the proper way see adobe documentation "(adopts default value if undefined during cascade)" in other words some properties of TLFTextFild may cascade verticalAlign properety like textFormat , so put verticalAlign property after every thing to cascade the others ,then it will workm . it works with me :)

    0 讨论(0)
  • 2020-12-16 01:17

    This works great only for the case that the text is in single line.

    First you have to add an initial break line in every text. I did it in a new component extending TextField and overriding the "text" function, adding the initial break line character.

    import flash.text.TextField;
    
    public class MyTextField extends TextField
    {
        public function MyTextField()
        {
            super();
        }
    
        public override function set text(value:String):void
        {
            value = "\n" + value;
    
            super.text = value;
        }
    }
    

    Then you need to apply format to the text, an use the "leading" property that represents the amount of vertical space between lines.

    myTextFormat = new TextFormat();
    
    // This is the existent horizontal align
    myTextFormat.align = TextFormatAlign.CENTER; 
    
    // This is my simulated vertical align. Remember that the first character 
    // is always a break line. In most cases it will be a negative value...
    myTextFormat.leading = -22;   
    
    var myTextField:MyTextField = new MyTextField();
    myTextField.text = "Hello";
    myTextField.setTextFormat(myTextFormat);
    

    I hope this will help to someone who needs vertical align in single line text using TextField. :-)

    0 讨论(0)
提交回复
热议问题