26
Aug
07

Changing a Button control’s text alignment and label placement

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 Responses to “Changing a Button control's text alignment and label placement”


  1. 1 Alexey Aug 28th, 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

  2. 2 Alexey Sep 1st, 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 :(

  3. 3 Sjoerd Feb 19th, 2008 at 2:13 am

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

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".