Setting the background color on a Spark SimpleText control in Flex 4

The following example shows how you can set the background color on a Spark SimpleText control in Flex 4 by setting the backgroundColor style.

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.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/06/26/setting-the-background-color-on-a-spark-simpletext-control-in-flex-4/ -->
<s:Application name="Spark_SimpleText_backgroundColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:mx="library://ns.adobe.com/flex/halo"
        xmlns:s="library://ns.adobe.com/flex/spark"
        creationComplete="init();">
 
    <fx:Script>
        <![CDATA[
            private function init():void {
                var arr:Array = ["Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
                                "Fusce id nisi in nisi pulvinar convallis.",
                                "Sed eu justo in nibh ultrices hendrerit.",
                                "Phasellus nec metus at libero pretium imperdiet nec quis libero.",
                                "Aenean nec turpis ut turpis lacinia laoreet.",
                                "Phasellus quis massa massa, ut aliquam massa.",
                                "Sed placerat purus at quam consectetur lacinia.",
                                "Phasellus blandit risus et eros varius pellentesque.",
                                "Sed aliquam purus eget quam consequat congue.",
                                "Nulla et sapien orci, eu ultricies magna.",
                                "Aenean et nisl quis mi dapibus lobortis.",
                                "Cras dapibus lacus eu ipsum blandit vel suscipit velit sagittis."];
                simpleTxt.text = arr.join("\n");
            }
        ]]>
    </fx:Script>
 
    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <mx:Form styleName="plain">
            <mx:FormItem label="backgroundColor:">
                <mx:ColorPicker id="colorPicker" selectedColor="white" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>
 
    <s:SimpleText id="simpleTxt"
            backgroundColor="{colorPicker.selectedColor}"
            horizontalCenter="0"
            verticalCenter="0" />
 
</s:Application>

You can also set the backgroundColor style in an external .CSS file or <Script/> block, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/06/26/setting-the-background-color-on-a-spark-simpletext-control-in-flex-4/ -->
<s:Application name="Spark_SimpleText_backgroundColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:mx="library://ns.adobe.com/flex/halo"
        xmlns:s="library://ns.adobe.com/flex/spark"
        creationComplete="init();">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/halo";
 
        s|SimpleText {
            backgroundColor: haloBlue;
        }
    </fx:Style>
 
    <fx:Script>
        <![CDATA[
            private function init():void {
                var arr:Array = ["Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
                    "Fusce id nisi in nisi pulvinar convallis.",
                    "Sed eu justo in nibh ultrices hendrerit.",
                    "Phasellus nec metus at libero pretium imperdiet nec quis libero.",
                    "Aenean nec turpis ut turpis lacinia laoreet.",
                    "Phasellus quis massa massa, ut aliquam massa.",
                    "Sed placerat purus at quam consectetur lacinia.",
                    "Phasellus blandit risus et eros varius pellentesque.",
                    "Sed aliquam purus eget quam consequat congue.",
                    "Nulla et sapien orci, eu ultricies magna.",
                    "Aenean et nisl quis mi dapibus lobortis.",
                    "Cras dapibus lacus eu ipsum blandit vel suscipit velit sagittis."];
                simpleTxt.text = arr.join("\n");
            }
        ]]>
    </fx:Script>
 
    <s:SimpleText id="simpleTxt"
            horizontalCenter="0"
            verticalCenter="0" />
 
</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/06/26/setting-the-background-color-on-a-spark-simpletext-control-in-flex-4/ -->
<s:Application name="Spark_SimpleText_backgroundColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:mx="library://ns.adobe.com/flex/halo"
        xmlns:s="library://ns.adobe.com/flex/spark"
        creationComplete="init();">
 
    <fx:Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;
 
            private function init():void {
                var arr:Array = ["Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
                    "Fusce id nisi in nisi pulvinar convallis.",
                    "Sed eu justo in nibh ultrices hendrerit.",
                    "Phasellus nec metus at libero pretium imperdiet nec quis libero.",
                    "Aenean nec turpis ut turpis lacinia laoreet.",
                    "Phasellus quis massa massa, ut aliquam massa.",
                    "Sed placerat purus at quam consectetur lacinia.",
                    "Phasellus blandit risus et eros varius pellentesque.",
                    "Sed aliquam purus eget quam consequat congue.",
                    "Nulla et sapien orci, eu ultricies magna.",
                    "Aenean et nisl quis mi dapibus lobortis.",
                    "Cras dapibus lacus eu ipsum blandit vel suscipit velit sagittis."];
                simpleTxt.text = arr.join("\n");
            }
 
            private function colorPicker_change(evt:ColorPickerEvent):void{
                simpleTxt.setStyle("backgroundColor", evt.color);
            }
        ]]>
    </fx:Script>
 
    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <mx:Form styleName="plain">
            <mx:FormItem label="backgroundColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="white"
                        change="colorPicker_change(event);"/>
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>
 
    <s:SimpleText id="simpleTxt"
            horizontalCenter="0"
            verticalCenter="0" />
 
</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/06/26/setting-the-background-color-on-a-spark-simpletext-control-in-flex-4/ -->
<s:Application name="Spark_SimpleText_backgroundColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:mx="library://ns.adobe.com/flex/halo"
        xmlns:s="library://ns.adobe.com/flex/spark"
        initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.ColorPicker;
            import mx.events.ColorPickerEvent;
            import spark.primitives.SimpleText;
 
            private var simpleTxt:SimpleText;
            private var colorPicker:ColorPicker;
 
            private function init():void {
                var arr:Array = ["Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
                    "Fusce id nisi in nisi pulvinar convallis.",
                    "Sed eu justo in nibh ultrices hendrerit.",
                    "Phasellus nec metus at libero pretium imperdiet nec quis libero.",
                    "Aenean nec turpis ut turpis lacinia laoreet.",
                    "Phasellus quis massa massa, ut aliquam massa.",
                    "Sed placerat purus at quam consectetur lacinia.",
                    "Phasellus blandit risus et eros varius pellentesque.",
                    "Sed aliquam purus eget quam consequat congue.",
                    "Nulla et sapien orci, eu ultricies magna.",
                    "Aenean et nisl quis mi dapibus lobortis.",
                    "Cras dapibus lacus eu ipsum blandit vel suscipit velit sagittis."];
 
                colorPicker = new ColorPicker();
                colorPicker.selectedColor = 0xFFFFFF; /* white */
                colorPicker.addEventListener(ColorPickerEvent.CHANGE, colorPicker_change);
 
                var formItem:FormItem = new FormItem();
                formItem.label = "backgroundColor:";
                formItem.addElement(colorPicker);
 
                var form:Form = new Form();
                form.styleName = "plain";
                form.addElement(formItem);
 
                var appControlBar:ApplicationControlBar = new ApplicationControlBar();
                appControlBar.percentWidth = 100;
                appControlBar.setStyle("cornerRadius", 0);
                appControlBar.addElement(form);
                addElementAt(appControlBar, 0);
 
                simpleTxt = new SimpleText();
                simpleTxt.text = arr.join("\n");
                simpleTxt.horizontalCenter = 0;
                simpleTxt.verticalCenter = 0;
                addElement(simpleTxt);
            }
 
            private function colorPicker_change(evt:ColorPickerEvent):void{
                simpleTxt.setStyle("backgroundColor", evt.color);
            }
        ]]>
    </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.

One thought on “Setting the background color on a Spark SimpleText control in Flex 4

  1. Hi,

    Please pardon me as I am not commenting on this post rather I am asking a Flex related question. Question is as follows.
    I have two flex applications and I want to drag n drop display objects
    for one application to another application. Is this possible? if yes
    please provide some pointers.

    Thanks,
    Avi

Comments are closed.