Setting the typographic case on a Spark TextArea control in Flex 4

The following example shows how you can set the typographic case on a Spark TextArea control in Flex 4 by setting the typographicCase style to one of the static constants in the flash.text.engine.TypographicCase class.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/20/setting-the-typographic-case-on-an-fxtextarea-control-in-flex-gumbo/ -->
<Application name="FxTextArea_typographicCase_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <Script>
        <![CDATA[
            import flash.text.engine.TypographicCase;
        ]]>
    </Script>

    <Declarations>
        <String id="lorem" source="data/lorem.html" />
        <Array id="arr">
            <Object label="{TypographicCase.CAPS}" />
            <Object label="{TypographicCase.CAPS_AND_SMALL_CAPS}" />
            <Object label="{TypographicCase.DEFAULT}" />
            <Object label="{TypographicCase.LOWERCASE}" />
            <Object label="{TypographicCase.SMALL_CAPS}" />
            <Object label="{TypographicCase.TITLE}" />
            <Object label="{TypographicCase.UPPERCASE}" />
        </Array>
    </Declarations>

    <ApplicationControlBar dock="true">
        <Form styleName="plain">
            <FormItem label="typographicCase:">
                <ComboBox id="comboBox"
                        dataProvider="{arr}"
                        selectedIndex="2" />
            </FormItem>
        </Form>
    </ApplicationControlBar>

    <FxTextArea id="fxTextArea"
            content="{lorem}"
            typographicCase="{comboBox.selectedItem.label}"
            widthInChars="80"
            marginTop="20" />

</Application>

View source is enabled in the following example.

You can also set the typographicCase style 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/20/setting-the-typographic-case-on-an-fxtextarea-control-in-flex-gumbo/ -->
<Application name="FxTextArea_typographicCase_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <Style>
        FxTextArea {
            typographicCase: "uppercase";
        }
    </Style>

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

    <FxTextArea id="fxTextArea"
            content="{lorem}"
            widthInChars="80"
            marginTop="20" />

</Application>

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/20/setting-the-typographic-case-on-an-fxtextarea-control-in-flex-gumbo/ -->
<Application name="FxTextArea_typographicCase_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <Script>
        <![CDATA[
            import flash.text.engine.TypographicCase;
            import mx.events.ListEvent;

            private function comboBox_change(evt:ListEvent):void {
                var value:String = comboBox.selectedItem.label;
                fxTextArea.setStyle("typographicCase", value);
            }
        ]]>
    </Script>

    <Declarations>
        <String id="lorem" source="data/lorem.html" />
        <Array id="arr">
            <Object label="{TypographicCase.CAPS}" />
            <Object label="{TypographicCase.CAPS_AND_SMALL_CAPS}" />
            <Object label="{TypographicCase.DEFAULT}" />
            <Object label="{TypographicCase.LOWERCASE}" />
            <Object label="{TypographicCase.SMALL_CAPS}" />
            <Object label="{TypographicCase.TITLE}" />
            <Object label="{TypographicCase.UPPERCASE}" />
        </Array>
    </Declarations>

    <ApplicationControlBar dock="true">
        <Form styleName="plain">
            <FormItem label="typographicCase:">
                <ComboBox id="comboBox"
                        dataProvider="{arr}"
                        selectedIndex="2"
                        change="comboBox_change(event);" />
            </FormItem>
        </Form>
    </ApplicationControlBar>

    <FxTextArea id="fxTextArea"
            content="{lorem}"
            widthInChars="80"
            marginTop="20" />

</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/20/setting-the-typographic-case-on-an-fxtextarea-control-in-flex-gumbo/ -->
<Application name="FxTextArea_typographicCase_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

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

    <Script>
        <![CDATA[
            import flash.text.engine.TypographicCase;
            import mx.components.FxTextArea;
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.ComboBox;
            import mx.events.ListEvent;

            private var comboBox:ComboBox;
            private var fxTextArea:FxTextArea;
            private var arr:Array;

            private function init():void {
                arr = [];
                arr.push({label:TypographicCase.CAPS});
                arr.push({label:TypographicCase.CAPS_AND_SMALL_CAPS});
                arr.push({label:TypographicCase.DEFAULT});
                arr.push({label:TypographicCase.LOWERCASE});
                arr.push({label:TypographicCase.SMALL_CAPS});
                arr.push({label:TypographicCase.TITLE});
                arr.push({label:TypographicCase.UPPERCASE});

                comboBox = new ComboBox();
                comboBox.dataProvider = arr;
                comboBox.selectedIndex = 2;
                comboBox.addEventListener(ListEvent.CHANGE,
                            comboBox_change);

                var formItem:FormItem = new FormItem();
                formItem.label = "typographicCase:";
                formItem.addChild(comboBox);

                var form:Form = new Form();
                form.styleName = "plain";
                form.addChild(formItem);

                var appControlBar:ApplicationControlBar;
                appControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.addChild(form);
                addChildAt(appControlBar, 0);

                fxTextArea = new FxTextArea();
                fxTextArea.content = lorem;
                fxTextArea.widthInChars = 80;
                fxTextArea.setStyle("marginTop", 20);
                addChild(fxTextArea);
            }

            private function comboBox_change(evt:ListEvent):void {
                var value:String = comboBox.selectedItem.label;
                fxTextArea.setStyle("typographicCase", value);
            }
        ]]>
    </Script>

</Application>

This entry is based on a beta version of the Flex 4 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 4 SDK.

For more information on the TypographicCase class in Flash Player 10, see http://livedocs.adobe.com/flex/gumbo/langref/flash/text/engine/TypographicCase.html.

4 thoughts on “Setting the typographic case on a Spark TextArea control in Flex 4

  1. Greg Jastrab,

    Not really a great reason, but FxApplication doesn’t currently support the ApplicationControlBar container.
    So, if you prefer to use the new FxApplication instead of the older Halo Application tag, simply remove the ApplicationControlBar container tag and you should be good to go.

    Peter

  2. Am I the only one having trouble getting title case to work? It seems to have no effect on a Spark Label or Text Area.

Comments are closed.