如何在Button标签中写入两行文本?

时间:2011-03-09 15:27:54

标签: flex mxml

对于Button的“标签”字段,有没有办法在多行显示文字?或者自动将文本“换行”在几行而不是切割它的一部分?

3 个答案:

答案 0 :(得分:3)

使用标准mx:Button是不可能的 - 您可以创建自己的自定义组件来扩展Button或查找已经创建了所需内容的其他组件。

例如,flexlib库包含一个CanvasButton,可以帮助: http://flexlib.googlecode.com/svn/trunk/docs/flexlib/controls/CanvasButton.html

或者,如果您希望自己创建并创建自己的自定义组件,此博客文章将向您展示如何: http://www.forestandthetrees.com/2008/03/11/flex-multiline-button/

答案 1 :(得分:1)

假设您使用的是Flex 4,则可以使用此组件:

<强> WrappingButton.as

package 
{
  import flash.text.TextFieldAutoSize;
  import mx.controls.Button;

  public class WrappingButton extends Button
  {

    public function WrappingButton()
    {
      super();
    }

    override protected function createChildren():void
    {
      super.createChildren();

      textField.multiline = true;
      textField.wordWrap = true;
      textField.autoSize = TextFieldAutoSize.CENTER;
    }

    override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
    {
      super.updateDisplayList(unscaledWidth, unscaledHeight);
      textField.y = (this.height - textField.height) >> 1;

      height = textField.height + getStyle("paddingTop") + getStyle("paddingBottom");
      if (height < this.minHeight)
      {
        this.height = this.minHeight
      }
    }
  }
}

答案 2 :(得分:0)

如果您使用的是Flex 4,请使用s:按钮,然后为其创建自定义外观。在皮肤内你会看到