Changing a Button control’s text alignment and label placement

by Peter deHaan on August 26, 2007

in Button

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.

{ 3 comments… read them below or add one }

1 Alexey August 28, 2007 at 11:10 am

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

Reply

2 Alexey September 1, 2007 at 1:54 am

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

Reply

3 Sjoerd February 19, 2008 at 2:13 am

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

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: