Looping over paragraphs in the FxTextArea control in Flex Gumbo

The following example shows how you can loop over the paragraphs in a Flex Gumbo FxTextArea control using the export() method and E4X.

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/06/looping-over-paragraphs-in-the-fxtextarea-control-in-flex-gumbo/ -->
<Application name="FxTextArea_export_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <Script>
        private function getParagraphs():void {
            var textFlowNS:Namespace = new Namespace("http://ns.adobe.com/textLayout/2008");

            var xml:XML = textArea.export();
            var xmlList:XMLList = xml.textFlowNS::p;
            var para:XML;
            for each (para in xmlList) {
                para.@color = getRandomColor();
            }
            textArea.content = xml;
        }

        private function getRandomColor():String {
            return "0x" + Math.round(Math.random() * 0xFFFFFF).toString(16);
        }
    </Script>

    <ApplicationControlBar dock="true">
        <FxButton label="Randomize paragraph colors"
                click="getParagraphs();" />
    </ApplicationControlBar>

    <FxTextArea id="textArea" width="240" height="160">
        <content>
            <p>The quick brown fox jumped over the lazy dog.</p>
            <p>The quick brown fox jumped over the lazy dog.</p>
            <p>The quick brown fox jumped over the lazy dog.</p>
            <p>The quick brown fox jumped over the lazy dog.</p>
            <p>The quick brown fox jumped over the lazy dog.</p>
            <p>The quick brown fox jumped over the lazy dog.</p>
            <p>The quick brown fox jumped over the lazy dog.</p>
            <p>The quick brown fox jumped over the lazy dog.</p>
            <p>The quick brown fox jumped over the lazy dog.</p>
            <p>The quick brown fox jumped over the lazy dog.</p>
            <p>The quick brown fox jumped over the lazy dog.</p>
            <p>The quick brown fox jumped over the lazy dog.</p>
            <p>The quick brown fox jumped over the lazy dog.</p>
            <p>The quick brown fox jumped over the lazy dog.</p>
            <p>The quick brown fox jumped over the lazy dog.</p>
        </content>
    </FxTextArea>

</Application>

View source is enabled in the following example.

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.

2 thoughts on “Looping over paragraphs in the FxTextArea control in Flex Gumbo

  1. How in the world do you programmaticly scroll the FxTextArea control? I cannot find the verticalScrollPosition anywhere in FxTextArea. I am using Flex Gumbo MAX 2008 Preview build 4.0.0.215018.

  2. Steve G,

    I’m not sure if this is the best way of doing it, but it seems to work. I don’t have the MAX Preview build installed, but I’m using Flex Builder 3 w/ SDK build 4.0.0.4250:

    <?xml version="1.0" encoding="utf-8"?>
    <Application name="FxTextArea_scroller_test"
            xmlns="http://ns.adobe.com/mxml/2009"
            layout="vertical"
            verticalAlign="middle"
            backgroundColor="white">
    
        <Script>
            <![CDATA[
                import mx.components.FxVScrollBar;
    
                private function init():void {
                    var vsb:FxVScrollBar = textArea.scroller.verticalScrollBar;
                    var min:Number = vsb.minimum;
                    var val:Number = vsb.value;
                    var max:Number = vsb.maximum;
                    lbl1.text = min.toString();
                    lbl2.text = val.toString();
                    lbl3.text = max.toString();
                    progressBar.setProgress(val, max);
                }
            ]]>
        </Script>
    
        <ApplicationControlBar dock="true">
            <Form styleName="plain">
                <FormItem label="minimum:">
                    <Label id="lbl1" />
                </FormItem>
                <FormItem label="value:">
                    <Label id="lbl2" />
                </FormItem>
                <FormItem label="maximum:">
                    <Label id="lbl3" />
                </FormItem>
                <FormItem>
                    <ProgressBar id="progressBar"
                            mode="manual"
                            label="%1 / %2 (%3%%)" />
                </FormItem>
            </Form>
        </ApplicationControlBar>
    
        <FxTextArea id="textArea"
                width="100%"
                height="100%"
                enterFrame="init();">
            <content>
                <String source="data/lorem.html" />
            </content>
        </FxTextArea>
    
    </Application>
    

    I posted this as a new example, “Determining the current scroll position on an FxTextArea control in Flex Gumbo”.

    Peter

Comments are closed.