Setting the break opportunity on an FxTextArea control in Flex Gumbo

The following example shows how you can set the break opportunity for a Flex Gumbo FxTextArea control by setting the breakOpportunity style to one of the static constants in the flash.text.engine.BreakOpportunity class.

Currently, there are four possible values:

  • BreakOpportunity.ALL: Treats all characters in the ContentElement object as mandatory line break opportunities. You can use this option to generate the shortest possible lines, which you can use to create text on a line or similar effects.
  • BreakOpportunity.ANY: Treats any character in the ContentElement object as a line break opportunity. This value is typically used when Roman text is embedded in Asian text and it is desirable for breaks to happen in the middle of words.
  • BreakOpportunity.AUTO: Bases line break opportunities on Unicode character properties. This setting implements the Unicode line breaking properties defined by the Unicode Standard Annex #14.
  • BreakOpportunity.NONE: Treats no characters in the ContentElement object as line break opportunities.

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/12/05/setting-the-break-opportunity-on-an-fxtextarea-control-in-flex-gumbo/ -->
<Application name="FxTextArea_breakOpportunity_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <Script>
        import flash.text.engine.BreakOpportunity;
    </Script>

    <ApplicationControlBar dock="true">
        <Form styleName="plain">
            <FormItem label="breakOpportunity:">
                <ComboBox id="comboBox" selectedIndex="2">
                    <dataProvider>
                        <Array>
                            <String>{BreakOpportunity.ALL}</String>
                            <String>{BreakOpportunity.ANY}</String>
                            <String>{BreakOpportunity.AUTO}</String>
                            <String>{BreakOpportunity.NONE}</String>
                        </Array>
                    </dataProvider>
                </ComboBox>
            </FormItem>
        </Form>
    </ApplicationControlBar>

    <FxTextArea id="textArea"
            breakOpportunity="{comboBox.selectedItem}"
            width="400"
            height="300">
        <content><String source="data/lorem.html" /></content>
    </FxTextArea>

</Application>

View source is enabled in the following example.

You can also set the breakOpportunity 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/12/05/setting-the-break-opportunity-on-an-fxtextarea-control-in-flex-gumbo/ -->
<Application name="FxTextArea_breakOpportunity_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <Style>
        FxTextArea {
            breakOpportunity: "any";
        }
    </Style>

    <FxTextArea id="textArea"
            width="400"
            height="300">
        <content><String source="data/lorem.html" /></content>
    </FxTextArea>

</Application>

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

View MXML

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

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

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

    <ApplicationControlBar dock="true">
        <Form styleName="plain">
            <FormItem label="breakOpportunity:">
                <ComboBox id="comboBox"
                        selectedIndex="2"
                        change="comboBox_change(event);">
                    <dataProvider>
                        <Array>
                            <String>{BreakOpportunity.ALL}</String>
                            <String>{BreakOpportunity.ANY}</String>
                            <String>{BreakOpportunity.AUTO}</String>
                            <String>{BreakOpportunity.NONE}</String>
                        </Array>
                    </dataProvider>
                </ComboBox>
            </FormItem>
        </Form>
    </ApplicationControlBar>

    <FxTextArea id="textArea"
            width="400"
            height="300">
        <content><String source="data/lorem.html" /></content>
    </FxTextArea>

</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/12/05/setting-the-break-opportunity-on-an-fxtextarea-control-in-flex-gumbo/ -->
<Application name="FxTextArea_breakOpportunity_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <Script>
        <![CDATA[
            import flash.text.engine.BreakOpportunity;

            import mx.components.FxTextArea
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.ComboBox;
            import mx.events.ListEvent;

            [Embed(source="data/lorem.html",
                        mimeType="application/octet-stream")]
            private const Lorem:Class;

            private var comboBox:ComboBox;
            private var textArea:FxTextArea;

            private function init():void {
                var arr:Array = [];
                arr.push(BreakOpportunity.ALL);
                arr.push(BreakOpportunity.ANY);
                arr.push(BreakOpportunity.AUTO);
                arr.push(BreakOpportunity.NONE);

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

                var formItem:FormItem = new FormItem();
                formItem.label = "breakOpportunity:";
                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);

                textArea = new FxTextArea();
                textArea.width = 400;
                textArea.height = 300;
                textArea.content = new Lorem();
                addChild(textArea);
            }

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

</Application>

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.