Styling the text selection format on a Spark TextArea control in Flex 4

The following example shows how you can style the text selection format on a Spark TextArea control in Flex 4 by setting the focusedSelectionFormat property on the TextFlow’s interactionManager to a new SelectionFormat object.

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/09/05/styling-the-text-selection-format-on-a-spark-textarea-control-in-flex-4/ -->
<s:Application name="Spark_ISelectionManager_focusedSelectionFormat_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">
 
    <fx:Script>
        <![CDATA[
            import flashx.textLayout.edit.ISelectionManager;
            import flashx.textLayout.edit.SelectionFormat;
 
            protected function txtFocusInHandler(evt:FocusEvent):void {
                var fSelFmt:ISelectionManager = evt.currentTarget.textFlow.interactionManager;
                fSelFmt.focusedSelectionFormat = new SelectionFormat(0x000000, 1.0, BlendMode.INVERT);
                evt.currentTarget.textFlow.interactionManager = fSelFmt;
            }
        ]]>
    </fx:Script>
 
    <s:TextArea id="ta"
            lineBreak="explicit"
            widthInChars="40" heightInLines="20"
            initialize="ta.text = describeType(ta).toXMLString();"
            focusIn="txtFocusInHandler(event);"
            horizontalCenter="0" verticalCenter="0" />
 
</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.

2 thoughts on “Styling the text selection format on a Spark TextArea control in Flex 4

  1. I’m not entirely satisfied with this solution. I’m thinking there is probably a better way to override the SelectionFormat globally rather than on each text control individually. I’ll update this post if I find a better solution.

    Peter

    1. Can use Skin appearance customization , so do not need to individually set each text control…

Comments are closed.