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

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

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/07/15/setting-the-background-alpha-on-a-spark-simpletext-control-in-flex-4/ -->
<s:Application name="Spark_SimpleText_backgroundAlpha_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">
 
    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <mx:Form styleName="plain">
            <mx:FormItem label="backgroundColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="white" />
            </mx:FormItem>
            <mx:FormItem label="backgroundAlpha:">
                <s:HSlider id="slider"
                        minimum="0.0"
                        maximum="1.0"
                        value="1.0" 
                        stepSize="0.1" 
                        snapInterval="0.1" 
                        liveDragging="true" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>
 
    <s:SimpleText id="smplTxt"
            backgroundAlpha="{slider.value}"
            backgroundColor="{colorPicker.selectedColor}"
            maxWidth="500"
            horizontalCenter="0"
            verticalCenter="0">
        <fx:String source="lorem.txt" />
    </s:SimpleText>
 
</s:Application>

You can also set the backgroundAlpha and backgroundColor styles in an external .CSS file or <Style /> block, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/07/15/setting-the-background-alpha-on-a-spark-simpletext-control-in-flex-4/ -->
<s:Application name="Spark_SimpleText_backgroundAlpha_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:Style>
        @namespace mx "library://ns.adobe.com/flex/halo";
        @namespace s "library://ns.adobe.com/flex/spark";
 
        s|SimpleText {
            backgroundAlpha: 0.2;
            backgroundColor: red;
        }
    </fx:Style>
 
    <s:SimpleText id="smplTxt"
            maxWidth="500"
            horizontalCenter="0"
            verticalCenter="0">
        <fx:String source="lorem.txt" />
    </s:SimpleText>
 
</s:Application>

Or, you can set the backgroundAlpha and backgroundColor styles using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/07/15/setting-the-background-alpha-on-a-spark-simpletext-control-in-flex-4/ -->
<s:Application name="Spark_SimpleText_backgroundAlpha_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 mx.events.ColorPickerEvent;
 
            protected function colorPicker_change(evt:ColorPickerEvent):void {
                smplTxt.setStyle("backgroundColor", evt.color);
            }
 
            protected function slider_change(event:Event):void {
                smplTxt.setStyle("backgroundAlpha", slider.value);
            }
        ]]>
    </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:FormItem label="backgroundAlpha:">
                <s:HSlider id="slider"
                        minimum="0.0"
                        maximum="1.0"
                        value="1.0" 
                        stepSize="0.1" 
                        snapInterval="0.1" 
                        liveDragging="true"
                        change="slider_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>
 
    <s:SimpleText id="smplTxt"
            maxWidth="500"
            horizontalCenter="0"
            verticalCenter="0">
        <fx:String source="lorem.txt" />
    </s:SimpleText>
 
</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.

3 thoughts on “Setting the background alpha on a Spark SimpleText control in Flex 4

  1. why am unable to use BitMapImage component SimpleText component in my Flashbuilder 4 and 4.5 trail versions

Comments are closed.