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 http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://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.

 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

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

  1. lance says:

    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. Peter deHaan says:

    Lance,

    I don’t believe that is possible in the current builds. It’s a known issue, but feel free to file a bug/enhancement and request it at http://bugs.adobe.com/flex/

    Peter

  3. alo says:

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

  4. JC says:

    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.

  5. sebgymn says:

    TextLayoutFormatValueHolder class is removed. Use TextLayoutFormat instead.

  6. Christen says:

    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.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree