Announcement

Collapse
No announcement yet.

[Solved] Resizing a button's height.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [Solved] Resizing a button's height.

    Hi,

    I've been trying to make a button resize by height instead of width but I can't seem to get it working correctly.

    The text field (the button label) resizes perfectly, if there's more than 1 line the whole button will move up to the correct position, but the graphical part of the button won't scale to the size of the text field.

    Is there something I'm missing in my code?

    Code:
    import gfx.controls.Button;
    
    class gfx.controls.Inv_Button extends Button
    {
    	private var _index:Number;
    
        public function Inv_Button()
        {
            super();
        }
    
    	/**
    	 * The ActionScript-only label parameter that sets the text directly, and assumes localization has been handled externally.
    	 */
    	public function get label():String { return _label; }
    	public function set label(value:String):Void {
    		_label = value;
    
    		// When the label changes, if autoSize is true, and there is a textField, we want to resize the component to fit the label.
    		// The only exception is when the label is set during initialization.
            if (initialized) {
    
               if (textField != null) {
    		// Set the text first
    		textField.text = _label;
    		textField.autoSize = "left";
                }
    
                if (autoSize != "none") { sizeIsInvalid = true; }
    
                updateAfterStateChange();
            }
    	}
    
    	/**
    	 * The ActionScript-only index parameter that sets the index of the button.
    	 */
    	public function get index():Number { return _index; }
    	public function set index(value:Number):Void {
    		_index = value;
    	}
    
    	// This method is fired after the state has changed to allow the component to ensure the state is up-to-date.  For instance, updating the contraints in Button.
    	private function updateAfterStateChange():Void {
    		// Redraw should only happen AFTER the initialization.
    		if (!initialized) { return; }
    		if (textField != null && _label != null) {
    
    			textField.text = _label;
    			textField.autoSize = "left";
    		}
    
    		validateNow();// Ensure that the width/height is up to date.
    		if (constraints != null) {
    			constraints.update(width, height);
    		}
    
    		dispatchEvent({type:"stateChange", state:state});
    	}
    
    	// Get the auto width of the component, determined by the text width and constraints metrics
    	private function calculateHeight():Number {
    		if (constraints == null) {
    			invalidate();
    			return 0;
    		}
    		var metrics:Object = constraints.getElement(textField).metrics;
    		var h:Number = textField.textHeight + metrics.bottom + metrics.top + 5; // We add 5 pixels to accommodate Flash's poor measurement of anti-aliased fonts.
    		return h;
    	}
    
    	/**
    	 * Realigns the component based on the autoSize property.
    	 * we don't want to change the width, only the height
    	 * so alignForAutoSize has been over written
    	 */
    	private function alignForAutoSize():Void {
            if (!initialized || _autoSize == "none" || textField == null)
                return;
    
    		var oldHeight = __height;
            height = calculateHeight();
    
    		// Adjust the _y so the bottom is aligned correctly
    		if (height != oldHeight){
    
    			var oldBottom = _y + oldHeight;
    			_y = oldBottom - __height;
    		}
        }
    }

    #2
    So it turns out I needed to use "textField.autoFit = true;" instead of "textField.autoSize = "left";" the button graphic also needs to have "Enable guides for 9-slice scaling" checked for it to resize correctly.

    It's still a little off but hopefully I can work around that.

    Here's the code is anyone is in need of it.

    Code:
    import gfx.controls.Button;
    
    class gfx.controls.Inv_Button extends Button
    {
    	private var _index:Number;
    
        public function Inv_Button()
        {
            super();
        }
    
    	/**
    	 * The ActionScript-only index parameter that sets the index of the button.
    	 */
    	public function get index():Number { return _index; }
    	public function set index(value:Number):Void {
    		_index = value;
    	}
    
    	// This method is fired after the state has changed to allow the component to ensure the state is up-to-date.  For instance, updating the contraints in Button.
    	private function updateAfterStateChange():Void {
    		// Redraw should only happen AFTER the initialization.
    		if (!initialized) { return; }
    		if (textField != null && _label != null) {
    
    			textField.text = _label;
    			textField.autoFit = true;
    		}
    
    		validateNow();// Ensure that the width/height is up to date.
    		if (constraints != null) {
    			constraints.update(width, height);
    		}
    		dispatchEvent({type:"stateChange", state:state});
    	}
    
    	// Get the auto height of the component, determined by the text height and constraints metrics
    	private function calculateHeight():Number {
    		if (constraints == null) {
    			invalidate();
    			return 0;
    		}
    
    		var metrics:Object = constraints.getElement(textField).metrics;
    		var h:Number = textField.textHeight + metrics.bottom + metrics.top + 5; // We add 5 pixels to accommodate Flash's poor measurement of anti-aliased fonts.
    		return h;
    	}
    
    	/**
    	 * Realigns the component based on the autoSize property.
    	 * we don't want to change the width, only the height
    	 * so alignForAutoSize has been over written
    	 */
    	private function alignForAutoSize():Void {
            if (!initialized || _autoSize == "none" || textField == null)
                return;
    
    		var oldHeight = __height;
            height = calculateHeight();
    
    		var oldBottom = _y + oldHeight;
    		_y = oldBottom - __height;
        }
    }

    Comment

    Working...
    X