In a previous example, “Setting the horizontal gap between an icon and label on a LinkButton control in Flex”, we saw how you could set the horizontal gap between the icon and label on a Flex LinkButton control by setting the horizontalGap style.

The following example shows how you can set the vertical gap between the icon and label on a Flex LinkButton control by setting the verticalGap style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/11/setting-the-vertical-gap-between-an-icon-and-label-on-a-linkbutton-control-in-flex/ -->
<mx:Application name="LinkButton_verticalGap_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="verticalGap:">
                <mx:HSlider id="slider"
                        minimum="0"
                        maximum="10"
                        value="2"
                        snapInterval="1"
                        tickInterval="1"
                        liveDragging="true" />
            </mx:FormItem>
            <mx:FormItem label="labelPlacement:">
                <mx:ComboBox id="comboBox"
                        dataProvider="[top,bottom]" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:LinkButton id="linkButton"
            label="LinkButton"
            icon="@Embed('assets/LinkButton.png')"
            verticalGap="{slider.value}"
            labelPlacement="{comboBox.selectedItem}" />

</mx:Application>

View source is enabled in the following example.

You can also set the verticalGap style in an external .CSS file or block, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/11/setting-the-vertical-gap-between-an-icon-and-label-on-a-linkbutton-control-in-flex/ -->
<mx:Application name="LinkButton_verticalGap_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        LinkButton {
            icon: Embed("assets/LinkButton.png");
            verticalGap: 10;
        }
    </mx:Style>

    <mx:LinkButton id="linkButton"
            label="LinkButton"
            labelPlacement="top" />

</mx:Application>

Or, you can set the verticalGap style using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/11/setting-the-vertical-gap-between-an-icon-and-label-on-a-linkbutton-control-in-flex/ -->
<mx:Application name="LinkButton_verticalGap_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.ListEvent;
            import mx.events.SliderEvent;

            private function slider_change(evt:SliderEvent):void {
                linkButton.setStyle("verticalGap", evt.value);
            }

            private function comboBox_change(evt:ListEvent):void {
                linkButton.labelPlacement = comboBox.selectedItem.toString();
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="verticalGap:">
                <mx:HSlider id="slider"
                        minimum="0"
                        maximum="10"
                        value="2"
                        snapInterval="1"
                        tickInterval="1"
                        liveDragging="true"
                        change="slider_change(event);" />
            </mx:FormItem>
            <mx:FormItem label="labelPlacement:">
                <mx:ComboBox id="comboBox"
                        dataProvider="[top,bottom]"
                        change="comboBox_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:LinkButton id="linkButton"
            label="LinkButton"
            icon="@Embed('assets/LinkButton.png')"
            labelPlacement="top" />

</mx:Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/11/setting-the-vertical-gap-between-an-icon-and-label-on-a-linkbutton-control-in-flex/ -->
<mx:Application name="LinkButton_verticalGap_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.ButtonLabelPlacement;
            import mx.controls.ComboBox;
            import mx.controls.HSlider;
            import mx.controls.LinkButton;
            import mx.events.ListEvent;
            import mx.events.SliderEvent;

            [Embed("assets/LinkButton.png")]
            private const linkButtonIcon:Class;

            private var slider:HSlider;
            private var comboBox:ComboBox;
            private var linkButton:LinkButton;

            private function init():void {
                var arr:Array = [];
                arr.push(ButtonLabelPlacement.TOP);
                arr.push(ButtonLabelPlacement.BOTTOM);

                slider = new HSlider();
                slider.minimum = 0;
                slider.maximum = 10;
                slider.value = 2;
                slider.snapInterval = 1;
                slider.tickInterval = 1;
                slider.liveDragging = true;
                slider.addEventListener(SliderEvent.CHANGE,
                            slider_change);

                comboBox = new ComboBox();
                comboBox.dataProvider = arr;
                comboBox.addEventListener(ListEvent.CHANGE,
                            comboBox_change);

                var formItem1:FormItem = new FormItem();
                formItem1.label = "verticalGap:";
                formItem1.addChild(slider);

                var formItem2:FormItem = new FormItem();
                formItem2.label = "labelPlacement:";
                formItem2.addChild(comboBox);

                var form:Form = new Form();
                form.styleName = "plain";
                form.addChild(formItem1);
                form.addChild(formItem2);

                var appControlBar:ApplicationControlBar;
                appControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.addChild(form);
                addChildAt(appControlBar, 0);

                linkButton = new LinkButton();
                linkButton.label = "LinkButton";
                linkButton.labelPlacement = ButtonLabelPlacement.TOP;
                linkButton.setStyle("icon", linkButtonIcon);
                addChild(linkButton);
            }

            private function slider_change(evt:SliderEvent):void {
                linkButton.setStyle("verticalGap", evt.value);
            }

            private function comboBox_change(evt:ListEvent):void {
                var value:String = comboBox.selectedItem.toString();
                linkButton.labelPlacement = value;
            }
        ]]>
    </mx:Script>

</mx:Application>
 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

2 Responses to Setting the vertical gap between an icon and label on a LinkButton control in Flex

  1. Eduardo Pittol says:

    It not compile. It have a problem with Embed.

  2. peterd says:

    Eduardo Pittol,

    What is the error? And do you have a “assets/LinkButton.png” file in your Flex project?

    Peter

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree