Wednesday, June 18, 2008

Getting a button's icon size

I often want to set the size of an button based on the size of its icon but access to the icon can only be done through protected methods, so I wrote this simple component to expose the icons dimensions.

UtilButton.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Button xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.core.mx_internal;
use namespace mx_internal;

public function get iconWidth():int {
return getCurrentIcon().width;
}

public function get iconHeight():int {
return getCurrentIcon().height;
}
]]>
</mx:Script>
</mx:Button>


Then the button's size can be set like this:

 
<comps:UtilButton id="closeButton"
icon="@Embed('assets/CloseX.png')"
width="{closeButton.iconWidth + 6}"
height="{closeButton.iconHeight + 6}"/>