The following example shows how you can left, center, right, or fully justify a block of text in the Flex TextArea control by setting the textAlign style.

Full code after the jump.

View MXML

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

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

            private function toggleButtonBar_itemClick(evt:ItemClickEvent):void {
                textArea.setStyle("textAlign", evt.label);
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:String>left</mx:String>
        <mx:String>center</mx:String>
        <mx:String>right</mx:String>
        <mx:String>justify</mx:String>
    </mx:Array>

    <mx:String id="lorem" source="lorem.txt" />

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="textAlign:">
                <mx:ToggleButtonBar id="toggleButtonBar"
                        dataProvider="{arr}"
                        selectedIndex="0"
                        itemClick="toggleButtonBar_itemClick(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:TextArea id="textArea"
            text="{lorem}"
            textAlign="left"
            width="100%"
            height="100%" />

</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.

6 Responses to Setting the text alignment in a TextArea control in Flex

  1. John says:

    Note: According to the Flex documetation (for both Flex 2 and 3) the only valid settings for the textAlign style are left, right and center. Indeed, in Flex Builder these are the only options that are presented when using auto-complete. As demonstrated in the example, however, justify does actually work as expected.

  2. Sean Baker says:

    Peter,

    Is there any way to vertical align the text in a TextArea? I really need to get that working somehow.

    Thanks,

    Sean Baker.

  3. peterd says:

    Sean Baker,

    I don’t believe there is a way to vertically align text in a TextArea control in Flex 3.

    Peter

  4. dave says:

    fyi, i could not get textarea vertically aligned also.

    Instead, i set an explicit height for the TextArea and wrapped the TextArea within a HBox with verticalAlign=middle .

  5. Peter deHaan says:

    In other news, the Flex Gumbo FxTextArea control does support vertical alignment.
    For more information, see “Aligning text vertically within an FxTextArea control in Flex Gumbo”.

    Peter

  6. pravin says:

    Hi,
    I am getting a weired problem. When I make the text justify. The textarea eats some letters from right side. Can anyone help me?

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