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

by Peter deHaan on October 20, 2008

in FxTextArea, beta

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 beta, check out the Adobe Flash Builder 4 page on the Adobe Labs site. To download the latest build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4. For instructions on using the beta Flex 4 SDK in Flex Builder 3, see "Using the beta Flex 4 SDK in Flex Builder 3".

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

{ 3 comments… read them below or add one }

1 Greg Jastrab October 21, 2008 at 7:04 am

Is there a reason you’re using Application as the root tag instead of FxApplication?

Reply

2 peterd October 21, 2008 at 7:14 am

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

Reply

3 Greg Jastrab October 21, 2008 at 8:12 am

Ah cool, thanks.

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: