Aligning text vertically within an FxTextArea control in Flex Gumbo

The following example shows how you can align text vertically within a Flex Gumbo FxTextArea control by setting the verticalAlign style.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3”.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/26/aligning-text-vertically-within-an-fxtextarea-control-in-flex-gumbo/ -->
<FxApplication name="FxTextArea_verticalAlign_test"
        xmlns="http://ns.adobe.com/mxml/2009">
    <layout>
        <BasicLayout />
    </layout>

    <Script>
        <![CDATA[
            import flashx.textLayout.formats.VerticalAlign;
        ]]>
    </Script>

    <Declarations>
        <String id="lorem" source="data/lorem.txt" />
        <Array id="arr">
            <String>{VerticalAlign.TOP}</String>
            <String>{VerticalAlign.MIDDLE}</String>
            <String>{VerticalAlign.BOTTOM}</String>
            <String>{VerticalAlign.JUSTIFY}</String>
        </Array>
    </Declarations>

    <Form>
        <FormItem label="verticalAlign:">
            <ComboBox id="comboBox"
                    dataProvider="{arr}" />
        </FormItem>
    </Form>

    <Group horizontalCenter="0" verticalCenter="0">
        <FxTextArea id="textArea"
                text="{lorem}"
                widthInChars="80"
                heightInLines="25"
                verticalAlign="{comboBox.selectedItem}" />
    </Group>

</FxApplication>

View source is enabled in the following example.

You can also set the verticalAlign style in an external .CSS file or <Style /> block, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/26/aligning-text-vertically-within-an-fxtextarea-control-in-flex-gumbo/ -->
<FxApplication name="FxTextArea_verticalAlign_test"
        xmlns="http://ns.adobe.com/mxml/2009">
    <layout>
        <BasicLayout />
    </layout>

    <Style>
        FxTextArea {
            verticalAlign: justify;
        }
    </Style>

    <Declarations>
        <String id="lorem" source="data/lorem.txt" />
    </Declarations>

    <Group horizontalCenter="0" verticalCenter="0">
        <FxTextArea id="textArea"
                text="{lorem}"
                widthInChars="80"
                heightInLines="25" />
    </Group>

</FxApplication>

Or, you can also set the verticalAlign style using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/26/aligning-text-vertically-within-an-fxtextarea-control-in-flex-gumbo/ -->
<FxApplication name="FxTextArea_verticalAlign_test"
        xmlns="http://ns.adobe.com/mxml/2009">
    <layout>
        <BasicLayout />
    </layout>

    <Script>
        <![CDATA[
            import flashx.textLayout.formats.VerticalAlign;
            import mx.events.ListEvent;

            private function comboBox_change(evt:ListEvent):void {
                var value:String = comboBox.selectedItem.toString();
                textArea.setStyle("verticalAlign", value);
                trace(describeType(VerticalAlign).toXMLString());
            }
        ]]>
    </Script>

    <Declarations>
        <String id="lorem" source="data/lorem.txt" />
        <Array id="arr">
            <String>{VerticalAlign.TOP}</String>
            <String>{VerticalAlign.MIDDLE}</String>
            <String>{VerticalAlign.BOTTOM}</String>
            <String>{VerticalAlign.JUSTIFY}</String>
        </Array>
    </Declarations>

    <Form>
        <FormItem label="verticalAlign:">
            <ComboBox id="comboBox"
                    dataProvider="{arr}"
                    change="comboBox_change(event);" />
        </FormItem>
    </Form>

    <Group horizontalCenter="0" verticalCenter="0">
        <FxTextArea id="textArea"
                text="{lorem}"
                widthInChars="80"
                heightInLines="25" />
    </Group>

</FxApplication>

This entry is based on a beta version of the Flex Gumbo SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex Gumbo SDK.

2 thoughts on “Aligning text vertically within an FxTextArea control in Flex Gumbo

  1. Hi,

    Can this vertical align be implemented in Flex3? Any ideas how to implement it in Flex3?

  2. For Flex 3, you need to write a small piece of code … Just wrap the TextArea in a VBox and make the size grows depending the text …

Comments are closed.