How to vertical align a TextField in AS3

前端 未结 4 991
轻奢々
轻奢々 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条回答
  •  猫巷女王i
    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. :-)

提交回复
热议问题