Creating strikethrough text on a TextGraphic control in Flex Gumbo

The following example shows how you can create strikethrough text on a Flex TextGraphic control in Gumbo by setting the lineThrough style.

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/10/24/creating-strikethrough-text-on-a-textgraphic-control-in-flex-gumbo/ -->
<FxApplication name="TextGraphic_lineThrough_test"
        xmlns="http://ns.adobe.com/mxml/2009">
    <layout>
        <BasicLayout />
    </layout>

    <Form>
        <FormItem label="lineThrough:">
            <FxCheckBox id="fxCheckBox"
                    selected="true" />
        </FormItem>
    </Form>

    <TextGraphic id="textGraphic"
            fontSize="24"
            lineThrough="{fxCheckBox.selected}"
            horizontalCenter="0"
            verticalCenter="0">
        <text>
            <![CDATA[The quick brown fox jumped over the lazy dog.]]>
        </text>
    </TextGraphic>

</FxApplication>

View source is enabled in the following example.

You can also set the lineThrough 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/10/24/creating-strikethrough-text-on-a-textgraphic-control-in-flex-gumbo/ -->
<FxApplication name="TextGraphic_lineThrough_test"
        xmlns="http://ns.adobe.com/mxml/2009">
    <layout>
        <BasicLayout />
    </layout>

    <Style>
        TextGraphic {
            fontSize: 24;
            lineThrough: true;
        }
    </Style>

    <TextGraphic id="textGraphic"
            horizontalCenter="0"
            verticalCenter="0">
        <text>
            <![CDATA[The quick brown fox jumped over the lazy dog.]]>
        </text>
    </TextGraphic>

</FxApplication>

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/24/creating-strikethrough-text-on-a-textgraphic-control-in-flex-gumbo/ -->
<FxApplication name="TextGraphic_lineThrough_test"
        xmlns="http://ns.adobe.com/mxml/2009">
    <layout>
        <BasicLayout />
    </layout>

    <Script>
        <![CDATA[
            private function fxCheckBox_change(evt:Event):void {
                var value:Boolean = fxCheckBox.selected
                textGraphic.setStyle("lineThrough", value);
            }
        ]]>
    </Script>

    <Form>
        <FormItem label="lineThrough:">
            <FxCheckBox id="fxCheckBox"
                    selected="true"
                    change="fxCheckBox_change(event);" />
        </FormItem>
    </Form>

    <TextGraphic id="textGraphic"
            fontSize="24"
            lineThrough="true"
            horizontalCenter="0"
            verticalCenter="0">
        <text>
            <![CDATA[The quick brown fox jumped over the lazy dog.]]>
        </text>
    </TextGraphic>

</FxApplication>

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/10/24/creating-strikethrough-text-on-a-textgraphic-control-in-flex-gumbo/ -->
<FxApplication name="TextGraphic_lineThrough_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        initialize="init();">
    <layout>
        <BasicLayout />
    </layout>

    <Script>
        <![CDATA[
            import mx.components.FxCheckBox;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.graphics.TextGraphic;

            private var fxCheckBox:FxCheckBox;
            private var textGraphic:TextGraphic;

            private function init():void {
                fxCheckBox = new FxCheckBox();
                fxCheckBox.selected = false;
                fxCheckBox.addEventListener(Event.CHANGE, fxCheckBox_change);

                var formItem:FormItem = new FormItem();
                formItem.label = "lineThrough:";
                formItem.addChild(fxCheckBox);

                var form:Form = new Form();
                form.addChild(formItem);
                addItem(form);

                textGraphic = new TextGraphic();
                textGraphic.text = "The quick brown fox jumped over the lazy dog.";
                textGraphic.setStyle("fontSize", 24);
                textGraphic.setStyle("lineThough", false);
                textGraphic.horizontalCenter = 0;
                textGraphic.verticalCenter = 0;
                addItem(textGraphic);
            }

            private function fxCheckBox_change(evt:Event):void {
                var value:Boolean = fxCheckBox.selected
                textGraphic.setStyle("lineThrough", value);
            }
        ]]>
    </Script>

</FxApplication>

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.

One thought on “Creating strikethrough text on a TextGraphic control in Flex Gumbo

  1. Peter,

    Thanks for the example.

    I have tried to run the above example in my FlashBuilder. Am I missing some library for running “TextGraphic”. Can you explain how to create or use FXG with my FlashBuilder 4.

Comments are closed.