Truncating text using the Spark SimpleText control in Flex 4

The following example shows how you can truncate text using the Spark SimpleText control in Flex 4 by setting the truncation property and specifying the maximum number of lines allowed.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex 4 SDK installed. To download the Flash Builder 4 beta, see http://labs.adobe.com/technologies/flashbuilder4/. To download the latest build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

View MXML

<?xml version="1.0" encoding="UTF-8"?>
<!-- http://blog.flexexamples.com/2009/06/03/truncating-text-using-the-spark-simpletext-control-in-flex-4/ -->
<s:Application name="Spark_SimpleText_truncation_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 flashx.textLayout.factory.TruncationOptions;
        ]]>
    </fx:Script>

    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <mx:Form styleName="plain">
            <mx:FormItem label="truncation:">
                <s:HSlider id="slider1"
                        minimum="{TruncationOptions.NO_LINE_COUNT_LIMIT}"
                        maximum="8"
                        value="-1"
                        liveDragging="true" />
            </mx:FormItem>
            <mx:FormItem label="width:">
                <s:HSlider id="slider2"
                        minimum="100"
                        maximum="300"
                        value="300"
                        liveDragging="true" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <s:Group horizontalCenter="0" verticalCenter="0">
        <s:Rect width="100%" height="100%">
            <s:fill>
                <s:SolidColor color="red" alpha="0.1" />
            </s:fill>
        </s:Rect>
        <s:SimpleText id="simpleTxt"
                text="The quick brown fox jumps over the lazy dog."
                fontSize="24"
                truncation="{slider1.value}"
                width="{slider2.value}" />
    </s:Group>

</s:Application>

View source is enabled in the following example.

You can also set the truncation property using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="UTF-8"?>
<!-- http://blog.flexexamples.com/2009/06/03/truncating-text-using-the-spark-simpletext-control-in-flex-4/ -->
<s:Application name="Spark_SimpleText_truncation_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 flashx.textLayout.factory.TruncationOptions;

            private function slider1_change(evt:Event):void {
                simpleTxt.truncation = slider1.value;
            }

            private function slider2_change(evt:Event):void {
                simpleTxt.width = slider2.value;
            }
        ]]>
    </fx:Script>

    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <mx:Form styleName="plain">
            <mx:FormItem label="truncation:">
                <s:HSlider id="slider1"
                        minimum="{TruncationOptions.NO_LINE_COUNT_LIMIT}"
                        maximum="8"
                        value="{TruncationOptions.NO_LINE_COUNT_LIMIT}"
                        liveDragging="true"
                        change="slider1_change(event);" />
            </mx:FormItem>
            <mx:FormItem label="width:">
                <s:HSlider id="slider2"
                        minimum="100"
                        maximum="300"
                        value="300"
                        liveDragging="true"
                        change="slider2_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <s:Group horizontalCenter="0" verticalCenter="0">
        <s:Rect width="100%" height="100%">
            <s:fill>
                <s:SolidColor color="red" alpha="0.1" />
            </s:fill>
        </s:Rect>
        <s:SimpleText id="simpleTxt"
                text="The quick brown fox jumps over the lazy dog."
                fontSize="24"
                width="300" />
    </s:Group>

</s: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/2009/06/03/truncating-text-using-the-spark-simpletext-control-in-flex-4/ -->
<s:Application name="Spark_SimpleText_truncation_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"
        creationComplete="init();">

    <fx:Script>
        <![CDATA[
            import flashx.textLayout.factory.TruncationOptions;

            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.graphics.SolidColor;

            import spark.components.Group;
            import spark.components.HSlider;
            import spark.primitives.Rect;
            import spark.primitives.SimpleText;

            private var slider1:HSlider;
            private var slider2:HSlider;
            private var simpleTxt:SimpleText;

            private function init():void {
                slider1 = new HSlider();
                slider1.minimum = TruncationOptions.NO_LINE_COUNT_LIMIT;
                slider1.maximum = 8;
                slider1.value = TruncationOptions.NO_LINE_COUNT_LIMIT;
                slider1.liveDragging = true;
                slider1.addEventListener(Event.CHANGE, slider1_change);

                slider2 = new HSlider();
                slider2.minimum = 100;
                slider2.maximum = 300;
                slider2.value = 300;
                slider2.liveDragging = true;
                slider2.addEventListener(Event.CHANGE, slider2_change);

                var formItem1:FormItem = new FormItem();
                formItem1.label = "truncation:";
                formItem1.addElement(slider1);

                var formItem2:FormItem = new FormItem();
                formItem2.label = "width:";
                formItem2.addElement(slider2);

                var form:Form = new Form();
                form.styleName = "plain";
                form.addElement(formItem1);
                form.addElement(formItem2);

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

                var rect:Rect = new Rect();
                rect.fill = new SolidColor(0xFF0000, 0.1); // red 10%
                rect.percentWidth = 100;
                rect.percentHeight = 100;

                simpleTxt = new SimpleText();
                simpleTxt.text = "The quick brown fox jumps over the lazy dog.";
                simpleTxt.width = slider2.value;
                simpleTxt.setStyle("fontSize", 24);

                var gr:Group = new Group();
                gr.addElement(rect);
                gr.addElement(simpleTxt);
                gr.horizontalCenter = 0;
                gr.verticalCenter = 0;
                addElement(gr);
            }

            private function slider1_change(evt:Event):void {
                simpleTxt.truncation = slider1.value;
            }

            private function slider2_change(evt:Event):void {
                simpleTxt.width = slider2.value;
            }
        ]]>
    </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 SDK.

One thought on “Truncating text using the Spark SimpleText control in Flex 4

  1. Thanks for post, Peter.

    I am using a spark TextArea component and programatically assigning some text to it. Sometimes, the last line does not fit into the given width and height of the control. Is there any way I can not show any lines that are partially displayed?

    Any advice is welcome.

    Thanks,

    Om

Comments are closed.