Globally styling anchor tags on a Spark RichEditableText control in Flex 4

The following example shows how you can globally style the normal and hover states of a hyperlink on a Flex 4 Spark RichEditableText control by setting the static TextFlow.defaultConfiguration 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/2009/03/17/globally-styling-anchor-tags-on-a-textview-control-in-flex-gumbo/ -->
<s:Application name="TextFlow_defaultConfiguration_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"
        preinitialize="init();">
 
    <fx:Script>
        import flashx.textLayout.elements.Configuration;
        import flashx.textLayout.elements.TextFlow;
        import flashx.textLayout.formats.TextDecoration;
        import flashx.textLayout.formats.TextLayoutFormatValueHolder;
 
        private function init():void {
            var cfg:Configuration = TextFlow.defaultConfiguration;
 
            var normalTLF:TextLayoutFormatValueHolder = new TextLayoutFormatValueHolder(cfg.defaultLinkNormalFormat);
            normalTLF.color = 0xFF0000;
 
            var hoverTLF:TextLayoutFormatValueHolder = new TextLayoutFormatValueHolder(cfg.defaultLinkHoverFormat);
            hoverTLF.color = 0xFF00FF;
            hoverTLF.textDecoration = TextDecoration.NONE;
 
            var activeTLF:TextLayoutFormatValueHolder = new TextLayoutFormatValueHolder(cfg.defaultLinkActiveFormat);
            activeTLF.color = 0x00FF00;
 
            cfg.defaultLinkNormalFormat = normalTLF;
            cfg.defaultLinkHoverFormat = hoverTLF;
            cfg.defaultLinkActiveFormat = activeTLF;
            TextFlow.defaultConfiguration = cfg;
        }
    </fx:Script>
 
    <s:RichEditableText editable="false" width="300" horizontalCenter="0" verticalCenter="0">
        <s:textFlow>
            <s:TextFlow>
                <s:p>In addition to <s:a href="http://blog.flexexamples.com/">FlexExamples.com</s:a> you can check out my other mediocre blogs at <s:a href="http://actionscriptexamples.com/">ActionScriptExamples.com</s:a> and <s:a href="http://airexamples.com/">AIRExamples.com</s:a>.</s:p>
            </s:TextFlow>
        </s:textFlow>
    </s:RichEditableText>
 
</s:Application>

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.

6 thoughts on “Globally styling anchor tags on a Spark RichEditableText control in Flex 4

  1. Hey!

    Any ideas how to apply a stylesheet to a TextFlow? Like the Label or Text components in Flex Halo…

    I just want to be able to do and have it figure out how to do the rest, is this possible?

    Thanks for your help.

    Best,
    Lance

  2. error 1: cfg.defaultLinkNormalFormat —>cfg.linkNormalFormatDefault
    error 2: cfg.defaultLinkHoverFormat —>cfg.linkHoverFormatDefault
    error 2: cfg.defaultLinkActiveFormat—>cfg.linkNormalFormatDefault

  3. Thanks for the sample.
    Is there anyway we can get rid of the hand cursor for just non-href anchor? Do we have any control on the hand cursor display in general?
    Thanks for any info.

  4. Is there a way to do the same with other html tags?

    I would realy like to add a stylesheet to a textarea like in the old days. Or some other way of controlling how the different element should work.

    I would like to define some bold text as well as defining the links globaly. But it seems like it is either, if I import html/tlf dynamicaly.

Comments are closed.