Creating a bitmap fill in a Spark TextArea control in Flex 4

The following example shows how you can set a bitmap fill in a Spark TextArea control in Flex 4 by creating a custom skin with a Spark BitmapFill object and setting the skinClass style.

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/2010/04/12/creating-a-bitmap-fill-in-a-spark-textarea-control-in-flex-4/ -->
<s:Application name="Spark_TextArea_skinClass_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
 
    <s:TextArea id="txtArea"
            skinClass="skins.CustomTextAreaSkin"
            widthInChars="20" heightInLines="5"
            horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/04/12/creating-a-bitmap-fill-in-a-spark-textarea-control-in-flex-4/ -->
<s:SparkSkin name="CustomTextAreaSkin"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:fb="http://ns.adobe.com/flashbuilder/2009"
        alpha.disabled="0.5"
        blendMode="normal">
 
    <!-- states -->
    <s:states>
        <s:State name="normal"/>
        <s:State name="disabled"/>
    </s:states>
 
    <fx:Metadata>
        [HostComponent("spark.components.TextArea")]
    </fx:Metadata> 
 
    <fx:Script fb:purpose="styling">
        <![CDATA[
            private var lineBreakChanged:Boolean;
            private var paddingChanged:Boolean;
            private var verticalAlignChanged:Boolean;
 
            /* 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 = [];
 
            override public function get contentItems():Array {
                return contentFill;
            }
 
            override protected function commitProperties():void {
                super.commitProperties();
 
                if (lineBreakChanged) {
                    updateStringStyle("lineBreak");
                    lineBreakChanged = false;
                }
                if (paddingChanged) {
                    updatePadding();
                    paddingChanged = false;
                }
                if (verticalAlignChanged) {
                    updateStringStyle("verticalAlign");
                    verticalAlignChanged = false;
                }
            }
 
            override protected function initializationComplete():void {
                useChromeColor = true;
                super.initializationComplete();
            }
 
            override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
                if (getStyle("borderVisible") == true) {
                    border.visible = true;
                    shadow.visible = true;
                    // background.left = background.top = background.right = background.bottom = 1;
                    textDisplay.left = textDisplay.top = textDisplay.right = textDisplay.bottom = 1;
                } else {
                    border.visible = false;
                    shadow.visible = false;
                    // background.left = background.top = background.right = background.bottom = 0;
                    textDisplay.left = textDisplay.top = textDisplay.right = textDisplay.bottom = 0;
                }
 
                borderStroke.color = getStyle("borderColor");
                borderStroke.alpha = getStyle("borderAlpha");
 
                super.updateDisplayList(unscaledWidth, unscaledHeight);
            }
 
            private function updatePadding():void {
                if (!textDisplay) {
                    return;
                }
 
                // Push padding styles into the textDisplay
                var padding:Number;
 
                padding = getStyle("paddingLeft");
                if (textDisplay.getStyle("paddingLeft") != padding) {
                    textDisplay.setStyle("paddingLeft", padding);
                }
 
                padding = getStyle("paddingTop");
                if (textDisplay.getStyle("paddingTop") != padding) {
                    textDisplay.setStyle("paddingTop", padding);
                }
 
                padding = getStyle("paddingRight");
                if (textDisplay.getStyle("paddingRight") != padding) {
                    textDisplay.setStyle("paddingRight", padding);
                }
 
                padding = getStyle("paddingBottom");
                if (textDisplay.getStyle("paddingBottom") != padding) {
                    textDisplay.setStyle("paddingBottom", padding);
                }
            }
 
            private function updateStringStyle(styleName:String):void {
                if (!textDisplay)
                    return;
 
                // Push style into the textDisplay
                var style:String;
 
                style = getStyle(styleName);
                if (textDisplay.getStyle(styleName) != style) {
                    textDisplay.setStyle(styleName, style);
                }
            }
 
            override public function styleChanged(styleProp:String):void {
                var allStyles:Boolean = !styleProp || styleProp == "styleName";
 
                super.styleChanged(styleProp);
 
                if (allStyles || styleProp.indexOf("lineBreak") == 0) {
                    lineBreakChanged = true;
                    invalidateProperties();
                }
                if (allStyles || styleProp.indexOf("padding") == 0) {
                    paddingChanged = true;
                    invalidateProperties();
                }
                if (allStyles || styleProp.indexOf("verticalAlign") == 0) {
                    verticalAlignChanged = true;
                    invalidateProperties();
                }
            }
        ]]>
    </fx:Script>
 
    <fx:Script>
        <![CDATA[
            private static const focusExclusions:Array = ["textDisplay"];
 
            override public function get focusSkinExclusions():Array {
                return focusExclusions;
            }
        ]]>
    </fx:Script>
 
    <!-- border -->
    <s:Rect id="border" left="0" right="0" top="0" bottom="0">
        <s:stroke>
            <s:SolidColorStroke id="borderStroke" 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>
            <s:BitmapFill id="bgFill" source="@Embed('pattern_143.gif')" alpha="0.5" fillMode="repeat" />
        </s:fill>
    </s:Rect>
 
    <!-- shadow -->
    <s:Rect id="shadow" left="1" top="1" right="1" height="1">
        <s:fill>
            <s:SolidColor color="0x000000" alpha="0.12" />
        </s:fill>
    </s:Rect>
 
    <!--- Defines the scroller that is used to scroll the TextArea control. -->
    <s:Scroller id="scroller" left="0" top="0" right="0" bottom="0" minViewportInset="1" measuredSizeIncludesScrollBars="false" hasFocusableChildren="false">
        <s:RichEditableText id="textDisplay"
                  heightInLines="10"
                  widthInChars="15" />
    </s:Scroller>
 
</s:SparkSkin>

[GoogleAdsWide]

View source is enabled in the following example.

[Squidfingers]

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 “Creating a bitmap fill in a Spark TextArea control in Flex 4

  1. It is a nice work and this is exactly the function i am looking for.

    But I got a runtime exception which is
    ReferenceError: Error #1069: Property bgFill not found on MyTextAreaSkin and there is no default value.

    Any solution for fixing this? Thank you very much.

Comments are closed.