Creating a transparent Spark TextArea control in Flex 4

The following example shows how you can create a semi-transparent Spark TextArea control in Flex 4 by creating a custom skin and setting the alpha property on the background fill.

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.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/06/02/creating-a-transparent-spark-textarea-control-in-flex-gumbo/ -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">

    <s:TextArea id="textArea"
            contentBackgroundColor="red"
            skinClass="skins.CustomTextAreaSkin"
            horizontalCenter="0"
            verticalCenter="0">
        <s:text>The quick brown fox jumps over the lazy dog.</s:text>
    </s:TextArea>

</s:Application>

And the custom skin class, skins/CustomTextAreaSkin.mxml, is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/06/02/creating-a-transparent-spark-textarea-control-in-flex-gumbo/ -->
<s:SparkSkin name="CustomTextAreaSkin"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        minWidth="36" minHeight="36"
        alpha.disabled="0.5">
    <s:states>
        <s:State name="normal"/>
        <s:State name="disabled"/>
    </s:states>

    <fx:Metadata>
    <![CDATA[
        [HostComponent("spark.components.TextArea")]
    ]]>
    </fx:Metadata>

    <fx:Script>
        /* Define the skin elements that should not be colorized.
           For text area, the skin itself is colorized but the individual parts are not. */
        static private const exclusions:Array = ["background", "scroller"];

        override public function get colorizeExclusions():Array {return exclusions;}

        /* Define the content fill items that should be colored by the "contentBackgroundColor" style. */
        static private const contentFill:Array = ["bgFill"];

        override public function get contentItems():Array {return contentFill};
    </fx:Script>

    <!-- border -->
    <s:Rect left="0" right="0" top="0" bottom="0">
        <s:stroke>
            <s:SolidColorStroke color="0x686868" weight="1"/>
        </s:stroke>
    </s:Rect>

    <!-- fill -->
    <!--- Defines the appearance of the TextArea component's background. -->
    <s:Rect id="background" left="1" right="1" top="1" bottom="1">
        <s:fill>
        <!--- Defines the background fill color. -->
            <s:SolidColor id="bgFill" color="0xFFFFFF" alpha="0.5" />
        </s:fill>
    </s:Rect>

    <!-- shadow -->
    <s:Rect left="1" top="1" right="1" height="1">
        <s:fill>
            <s:SolidColor color="0x000000" alpha="0.12" />
        </s:fill>
    </s:Rect>

    <!--- Defines the scroller used to scroll the RichEditableText. -->
    <s:Scroller id="scroller" left="0" top="0" right="0" bottom="0">
        <s:RichEditableText id="textView"
                  heightInLines="10"
                  paddingLeft="4" paddingTop="4"
                  paddingRight="4" paddingBottom="4"/>
    </s:Scroller>

</s:SparkSkin>

[GoogleAdsWide]

View source is enabled in the following example.

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.

2 thoughts on “Creating a transparent Spark TextArea control in Flex 4

  1. Hi Peter,

    I’m trying to use this example, but get different results (using sdk 4.0.0.9237).
    The width and height act very weird (not as in the example), do you have any idea why?

  2. There is simpler way to control “alpha” value of Spark TextArea’s background.

    Just set the value of “contentBackgroundAlpha” style on Spark TextArea to the desired value from 0 to 1.

    textArea.contentBackgroundAlpha = 0.5

Comments are closed.