Setting word wrapping on the Spark TextArea control in Flex 4

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

8 thoughts on “Setting word wrapping on the Spark TextArea control in Flex 4

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

  2. Hello Peter.

    Could you clarify for me one question: the escape character – ‘\n’ – does break the line of text in ActionScript block, but I’d like to use the multiline property of Spark Label to break the lines of text as I need it within MXML. Something like this:

    <s:Label text="First line n Second line" />

    Whether I put it with preceding ‘\’ or plainly or wrap it in single quotes I just see it faithfully reproduced as a text. Managing this with ‘width’ of a Label, or constraints like ‘left’ or ‘right’ is rather inconvenient.

    Thanks,
    Igor

    1. @Igor,

      You need to wrap the “\n” with data binding:

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx">
       
          <s:Label text="First line{'n'}Second line" />
       
      </s:Application>

      I’ll see if I can find the SDK/compiler bug for this. I think there was another workaround.

      Peter

      1. Well, I found at least 4 versions of this reported in JIRA. I believe the last one (SDK-12649) is the bug I was remembering:

        http://bugs.adobe.com/jira/browse/SDK-266 – “Can’t insert newline in text without using htmlText”
        http://bugs.adobe.com/jira/browse/SDK-1352 – “need a way to insert a newline character in MXML String values”
        http://bugs.adobe.com/jira/browse/SDK-8963 – “textarea component \n \r does not work”
        http://bugs.adobe.com/jira/browse/SDK-11469 – “Line breaks don’t work from attribute text, but works for AS and Binding. Should be consistent.”
        http://bugs.adobe.com/jira/browse/SDK-12649 – “Erratic behavior in line break display”

        Feel free to vote for all those bugs in JIRA so Adobe knows this issue is important to you and we can prioritize our bugs accordingly.

        Thanks,
        Peter

    2. This also seems to work:

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx">
       
          <s:Label text="First line&#13;Second line" />
       
      </s:Application>

      Peter

      1. Dear Peter,
        Thank you so much. Both syntaxes work, and that’s what matters most (at least at the moment). I’ll add my vote for fixing the problem.
        Just mater of curiosity: where could I find the explanations of why binding (this is not actual binding, is it?) to an escape character works?
        Igor

      2. I believe this is data binding, you’re just binding to a static string (“\n”). I think it gets treated by the compiler differently, or is causing it to get set by ActionScript in the internals of the Flex architecture (but what do I know, that may all be wrong).
        I read through each of those 5 bugs, and the best explanation is given in SDK-11469 and there is an additional workaround given in SDK-12649. And actually, another simple workaround for this is to set the text property via ActionScript instead of directly in MXML. It may also be marginally better performance wise, then using some binding to the static “\n” string. But yes, I do believe this is a compiler issue where it is seeing a “\n” and escaping the backslash so it gets preserved. Based on the 5 bugs, I was never 100% certain whether it was a bug or fact of life or what, but I definitely think it is a usability issue since it seems to be coming up again and again by internal and external people. I’m hoping that we can get http://bugs.adobe.com/jira/browse/SDK-12649 investigated by a current member of the compiler team to get a definitive answer on what the problem and solution (if any) is, but for now, it seems like the various workarounds work.

        If you want to see what is happening within Flex, it should be relatively easy to add the -keep compiler option to a simple “hello\nworld” test case and see what the generated code is.

        Peter

Comments are closed.