Setting the text alignment on a TabBar control in Flex

The following example shows how you can set the text alignment on a Flex TabBar control by setting the tabStyleName, tabWidth, and textAlign styles.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/05/setting-the-text-alignment-on-a-tabbar-control-in-flex/ -->
<mx:Application name="TabBar_tabStyleName_textAlign_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        .customTabStyleName {
            textAlign: center;
        }
    </mx:Style>

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

            private function comboBox_change(evt:ListEvent):void {
                var cssObj:CSSStyleDeclaration = StyleManager.getStyleDeclaration(".customTabStyleName");
                cssObj.setStyle("textAlign", comboBox.selectedItem.label);
            }
        ]]>
    </mx:Script>

    <mx:Array id="comboBoxDP">
        <mx:Object label="left" />
        <mx:Object label="center" />
        <mx:Object label="right" />
    </mx:Array>

    <mx:Array id="tabBarDP">
        <mx:Object label="Red" />
        <mx:Object label="Orange" />
        <mx:Object label="Yellow" />
        <mx:Object label="Green" />
        <mx:Object label="Blue" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="textAlign:">
                <mx:ComboBox id="comboBox"
                        dataProvider="{comboBoxDP}"
                        selectedIndex="1"
                        change="comboBox_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:TabBar id="tabBar"
            dataProvider="{tabBarDP}"
            tabWidth="120"
            tabStyleName="customTabStyleName" />

</mx:Application>

View source is enabled in the following example.

You can also set the tabStyleName and textAlign styles in an external .CSS file or <mx:Style /> block, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/05/setting-the-text-alignment-on-a-tabbar-control-in-flex/ -->
<mx:Application name="TabBar_tabStyleName_textAlign_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        TabBar {
            tabStyleName: customTabStyleName;
            tabWidth: 120;
        }

        .customTabStyleName {
            textAlign: left;
        }
    </mx:Style>

    <mx:Array id="arr">
        <mx:Object label="Red" />
        <mx:Object label="Orange" />
        <mx:Object label="Yellow" />
        <mx:Object label="Green" />
        <mx:Object label="Blue" />
    </mx:Array>

    <mx:TabBar id="tabBar"
            dataProvider="{arr}" />

</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/10/05/setting-the-text-alignment-on-a-tabbar-control-in-flex/ -->
<mx:Application name="TabBar_tabStyleName_textAlign_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.controls.TabBar;

            private var arr:Array;
            private var tabBar:TabBar;
            private var customTabStyleName:CSSStyleDeclaration;

            private function init():void {
                arr = [];
                arr.push({label:"Red"});
                arr.push({label:"Orange"});
                arr.push({label:"Yellow"});
                arr.push({label:"Green"});
                arr.push({label:"Blue"});

                customTabStyleName = new CSSStyleDeclaration(".customTabStyleName");
                customTabStyleName.setStyle("textAlign", "left");

                tabBar = new TabBar();
                tabBar.dataProvider = arr;
                tabBar.setStyle("tabWidth", 120);
                tabBar.setStyle("tabStyleName", "customTabStyleName");
                addChild(tabBar);
            }
        ]]>
    </mx:Script>

</mx:Application>