Setting word wrapping on the Spark TextArea control in Flex 4

by Peter deHaan on February 4, 2009

in Text, TextArea (Spark), beta

The following example shows how you can set word wrapping on a Flex 4 Spark TextArea control by setting the lineBreak style to one of the static constants in the LineBreak class (flashx.textLayout.formats.LineBreak):

  • toFit (LineBreak.TO_FIT): For lines that wrap to fit to the container width.
  • explicit (LineBreak.EXPLICIT): For lines that break only at explicit return/line feeds.

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/2009/02/04/setting-word-wrapping-on-the-fxtextarea-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextArea_lineBreak_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">

    <mx:ApplicationControlBar cornerRadius="0" width="100%">
        <mx:Form styleName="plain">
            <mx:FormItem label="lineBreak:">
                <s:DropDownList id="comboBox" requiresSelection="true">
                    <s:dataProvider>
                        <s:ArrayList source="[toFit,explicit]" />
                    </s:dataProvider>
                </s:DropDownList>
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <s:TextArea id="textArea"
            lineBreak="{comboBox.selectedItem}"
            fontSize="16"
            horizontalCenter="0"
            verticalCenter="0">
        <s:content>
            <s:p>1. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>2. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>3. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>4. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>5. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>6. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>7. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>8. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>9. The quick brown fox jumps over the lazy dog.</s:p>
        </s:content>
    </s:TextArea>

</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/04/setting-word-wrapping-on-the-fxtextarea-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextArea_lineBreak_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">

    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";

        s|TextArea {
            lineBreak: explicit;
        }
    </fx:Style>

    <s:TextArea id="textArea"
            fontSize="16"
            horizontalCenter="0"
            verticalCenter="0">
        <s:content>
            <s:p>1. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>2. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>3. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>4. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>5. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>6. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>7. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>8. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>9. The quick brown fox jumps over the lazy dog.</s:p>
        </s:content>
    </s:TextArea>

</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/04/setting-word-wrapping-on-the-fxtextarea-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextArea_lineBreak_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
    <fx:Script>
        <![CDATA[
            import mx.events.IndexChangedEvent;

            private function comboBox_selectionChanged(evt:IndexChangedEvent):void {
                textArea.setStyle("lineBreak", comboBox.selectedItem);
            }
        ]]>
    </fx:Script>

    <mx:ApplicationControlBar cornerRadius="0" width="100%">
        <mx:Form styleName="plain">
            <mx:FormItem label="lineBreak:">
                <s:DropDownList id="comboBox"
                        requiresSelection="true"
                        selectionChanged="comboBox_selectionChanged(event);">
                    <s:dataProvider>
                        <s:ArrayList source="[toFit,explicit]" />
                    </s:dataProvider>
                </s:DropDownList>
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <s:TextArea id="textArea"
            fontSize="16"
            horizontalCenter="0"
            verticalCenter="0">
        <s:content>
            <s:p>1. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>2. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>3. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>4. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>5. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>6. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>7. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>8. The quick brown fox jumps over the lazy dog.</s:p>
            <s:p>9. The quick brown fox jumps over the lazy dog.</s:p>
        </s:content>
    </s:TextArea>

</s:Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/04/setting-word-wrapping-on-the-fxtextarea-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextArea_lineBreak_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo"
        initialize="init();">

    <fx:Script>
        <![CDATA[
            import flashx.textLayout.formats.LineBreak;

            import mx.collections.ArrayList;
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.events.IndexChangedEvent;
            import spark.components.DropDownList;
            import spark.components.TextArea;

            private var comboBox:DropDownList;
            private var textArea:TextArea;

            private function init():void {
                var arr:Array = [LineBreak.TO_FIT, LineBreak.EXPLICIT];

                comboBox = new DropDownList();
                comboBox.dataProvider = new ArrayList(arr);
                comboBox.requiresSelection = true;
                comboBox.addEventListener(IndexChangedEvent.SELECTION_CHANGED, comboBox_change);

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

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

                var appControlBar:ApplicationControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.percentWidth = 100;
                appControlBar.setStyle("cornerRadius", 0);
                appControlBar.addChild(form);
                addElementAt(appControlBar, 0);

                var c:XML = <TextFlow xmlns="http://ns.adobe.com/textLayout/2008">
                        <p>1. The quick brown fox jumps over the lazy dog.</p>
                        <p>2. The quick brown fox jumps over the lazy dog.</p>
                        <p>3. The quick brown fox jumps over the lazy dog.</p>
                        <p>4. The quick brown fox jumps over the lazy dog.</p>
                        <p>5. The quick brown fox jumps over the lazy dog.</p>
                        <p>6. The quick brown fox jumps over the lazy dog.</p>
                        <p>7. The quick brown fox jumps over the lazy dog.</p>
                        <p>8. The quick brown fox jumps over the lazy dog.</p>
                        <p>9. The quick brown fox jumps over the lazy dog.</p>
                    </TextFlow>;

                textArea = new TextArea();
                textArea.content = c;
                textArea.setStyle("fontSize", 16);
                textArea.horizontalCenter = 0;
                textArea.verticalCenter = 0;
                addElement(textArea);
            }

            private function comboBox_change(evt:IndexChangedEvent):void {
                textArea.setStyle("lineBreak", comboBox.selectedItem);
            }
        ]]>
    </fx:Script>

</s:Application>

Also, if you were using plain text instead of HTML formatted text (using the <p> and/or <br> tags) you would set the text property and use the “\n” escape sequence to create new lines, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/04/setting-word-wrapping-on-the-fxtextarea-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextArea_lineBreak_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo"
        initialize="init();">

    <fx:Script>
        <![CDATA[
            import flashx.textLayout.formats.LineBreak;

            import mx.collections.ArrayList;
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.events.IndexChangedEvent;
            import spark.components.DropDownList;
            import spark.components.TextArea;

            private var comboBox:DropDownList;
            private var textArea:TextArea;

            private function init():void {
                var arr:Array = [LineBreak.TO_FIT, LineBreak.EXPLICIT];

                comboBox = new DropDownList();
                comboBox.dataProvider = new ArrayList(arr);
                comboBox.requiresSelection = true;
                comboBox.addEventListener(IndexChangedEvent.SELECTION_CHANGED, comboBox_change);

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

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

                var appControlBar:ApplicationControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.percentWidth = 100;
                appControlBar.setStyle("cornerRadius", 0);
                appControlBar.addChild(form);
                addElementAt(appControlBar, 0);

                var c:String = "1. The quick brown fox jumps over the lazy dog.\\n" +
                        "2. The quick brown fox jumps over the lazy dog.\\n" +
                        "3. The quick brown fox jumps over the lazy dog.\\n" +
                        "4. The quick brown fox jumps over the lazy dog.\\n" +
                        "5. The quick brown fox jumps over the lazy dog.\\n" +
                        "6. The quick brown fox jumps over the lazy dog.\\n" +
                        "7. The quick brown fox jumps over the lazy dog.\\n" +
                        "8. The quick brown fox jumps over the lazy dog.\\n" +
                        "9. The quick brown fox jumps over the lazy dog.";

                textArea = new TextArea();
                textArea.text = c;
                textArea.setStyle("fontSize", 16);
                textArea.horizontalCenter = 0;
                textArea.verticalCenter = 0;
                addElement(textArea);
            }

            private function comboBox_change(evt:IndexChangedEvent):void {
                textArea.setStyle("lineBreak", comboBox.selectedItem);
            }
        ]]>
    </fx:Script>

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

{ 2 comments… read them below or add one }

1 Andrei April 16, 2009 at 11:46 am

Hi,

I’ve tried this example in flexbuilder build 3.0.205647 with flex gumbo build 4.0.0.4904
and it didn’t work.
I linked the flashplayer 10 swc in the project’s flex build path.
It compiled fine but when i opened it in the browser it doesn’t display anything.
I right clicked in the window and the flash player is there.
What am i missing?

Reply

2 Peter deHaan May 10, 2009 at 8:54 am

Example(s) updated to work with Flex SDK 4.0.0.6731.

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: