The following example shows how you can use the Button class’s textAlign style and labelPlacement property to control the text layout in a Button control.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/26/changing-a-button-controls-text-alignment-and-label-placement/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            [Bindable]
            [Embed(source='assets/brick.png')]
            private var BrickIcon:Class;
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Label text="textAlign:" />
        <mx:ComboBox id="textAlignCB" selectedIndex="1">
            <mx:dataProvider>
                <mx:String>left</mx:String>
                <mx:String>center</mx:String>
                <mx:String>right</mx:String>
            </mx:dataProvider>
        </mx:ComboBox>

        <mx:Spacer width="50" />

        <mx:Label text="labelPlacement:" />
        <mx:ComboBox id="labelPlacementCB" selectedIndex="1">
            <mx:dataProvider>
                <mx:String>left</mx:String>
                <mx:String>right</mx:String>
                <mx:String>top</mx:String>
                <mx:String>bottom</mx:String>
            </mx:dataProvider>
        </mx:ComboBox>
    </mx:ApplicationControlBar>

    <mx:HBox>
        <mx:Button label="Button"
                textAlign="{textAlignCB.selectedItem}"
                width="120"
                paddingTop="5"
                paddingBottom="5" />

        <mx:Button label="Button"
                icon="{BrickIcon}"
                textAlign="{textAlignCB.selectedItem}"
                labelPlacement="{labelPlacementCB.selectedItem}"
                width="120"
                paddingTop="5"
                paddingBottom="5"
                verticalGap="0" />
    </mx:HBox>

</mx:Application>

View source is enabled in the following example.

 
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.

4 Responses to Changing a Button control’s text alignment and label placement

  1. Alexey says:

    I’ve download and run this example, but icon didn’t align with text. See screenshot.
    I’m using flex builder 2.0.1

  2. Alexey says:

    I’ve try this example on flex builder 3 M2 beta 1, but got the same result – icon didn’t centered with text :(

  3. Sjoerd says:

    And now is the question how to align the text Left, and the icon right.

  4. Demian says:

    Hi, i’ve got same question of Sjoerd:

    How can i align the text / label on the Left while having the icon align on the right?

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