package { import fl.controls.Button; import fl.core.InvalidationType; import flash.text.TextFieldAutoSize; public class AutoSizeButton extends Button { public function AutoSizeButton() { super(); } protected var _autoSize:Boolean = true; /** * Whether auto sizing is turned on. Default is true. */ public function get autoSize():Boolean { return _autoSize; } public function set autoSize(value:Boolean):void { _autoSize = value; textField.autoSize = _autoSize ? TextFieldAutoSize.LEFT : TextFieldAutoSize.NONE; invalidate(InvalidationType.SIZE); } override protected function drawLayout():void { if (autoSize) { // Set the component width by calculating // text & padding widths var txtPad:Number = Number(getStyleValue("textPadding")); _width = (textField.textWidth + 4) + (2 * txtPad); } super.drawLayout(); } } }