22
Mar
08

Applying a cascading style sheet to a TextArea control in Flex

The following example shows how you can dynamically load a CSS file and apply the style sheet to a Flex TextArea control by setting the styleSheet property.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/22/applying-a-cascading-style-sheet-to-a-textarea-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;

            private var styleSheet:StyleSheet;
            private var urlLoader:URLLoader;

            private function init():void {
                urlLoader = new URLLoader();
                urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
                urlLoader.load(new URLRequest("styles.css"));
            }

            private function urlLoader_complete(evt:Event):void {
                var css:String = URLLoader(evt.currentTarget).data;
                // Convert text to style sheet.
                styleSheet = new StyleSheet();
                styleSheet.parseCSS(css);
                // Set the style sheet.
                textArea.styleSheet = styleSheet;
            }

            private function textArea_link(evt:TextEvent):void {
                Alert.show("text: " + evt.text, "Panel");
            }
        ]]>
    </mx:Script>

    <mx:String id="txt" source="text.html" />

    <mx:TextArea id="textArea"
            htmlText="{txt}"
            editable="false"
            condenseWhite="true"
            width="100%"
            height="100%"
            link="textArea_link(event);" />

</mx:Application>

/src/styles.css

h1 {
    font-size: 24;
}

p {
    font-size: 10;
}

a {
    text-decoration: underline;
}

a:hover {
    color: #FF0000;
}

View source is enabled in the following example.

You can also dynamically load the external .CSS file using the <mx:HTTPService /> tag, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/22/applying-a-cascading-style-sheet-to-a-textarea-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Script>
        <![CDATA[
            import mx.rpc.events.ResultEvent;
            import mx.controls.Alert;

            private var styleSheet:StyleSheet;

            private function init():void {
                httpService.send();
            }

            private function httpService_result(evt:ResultEvent):void {
                var css:String = evt.result as String;
                // Convert text to style sheet.
                styleSheet = new StyleSheet();
                styleSheet.parseCSS(css);
                // Set the style sheet.
                textArea.styleSheet = styleSheet;
            }

            private function textArea_link(evt:TextEvent):void {
                Alert.show("text: " + evt.text, "Panel");
            }
        ]]>
    </mx:Script>

    <mx:HTTPService id="httpService"
            url="styles.css"
            resultFormat="text"
            result="httpService_result(event);" />
    <mx:String id="txt" source="text.html" />

    <mx:TextArea id="textArea"
            htmlText="{txt}"
            editable="false"
            condenseWhite="true"
            width="100%"
            height="100%"
            link="textArea_link(event);" />

</mx:Application>

/src/styles.css

h1 {
    font-size: 24;
}

p {
    font-size: 10;
    text-align: justify;
}

a {
    text-decoration: underline;
    color: #0000FF;
}

a:hover {
    color: #FF0000;
}

View source is enabled in the following example.


6 Responses to “Applying a cascading style sheet to a TextArea control in Flex”


  1. 1 David Buhler Mar 22nd, 2008 at 1:54 pm

    I would be curious to see if the same styles could be applied to a Rich Text Editor. My understanding is that they cannot be. The best I’ve come up with is to apply a tag along with a color attribute, which presents limitations.

    We’ve found that the inability in the RTE to display hyperlinks leads to a lot of confusion.

    Useful demo of the TA styles (and much appreciated), though.

  2. 2 David Buhler Mar 22nd, 2008 at 2:00 pm

    Seems the RegEx for the blog ate part of my comment…I was using the underline tag in HTML to create the look of a hyperlink.

  3. 3 Steve Mar 24th, 2008 at 5:20 am

    Thanks for another superb flex example. I have tried using this method with the textarea of a richTextEditor component. I get an error due to the fact that the input text is obviously editable . Is there anyway of applying this method to editable text or is it the fact that it calls the stylesheet at runtime is the problem and therefore would need to parse the css again after editing text? Hope you can help.

    Steve

  4. 4 JP Venter Apr 2nd, 2008 at 3:52 am

    Hi PeterD,

    I have a situation with external css and a textarea control which is populated with html (styled with classes) drawn from a database. Building on from your example above, the textarea is initially styled correctly. But try loading in new text to replace the text in the textarea after having scrolled. For me the styling bombs out, but what is weird is that it is only after a scroll of the textarea has occurred. I am using flex builder 3.

    Do have any idea why this may be happening?

    Thanks, JP

  5. 5 peterd Apr 2nd, 2008 at 7:29 am

    JP Venter,

    Does setting the styleSheet property again after setting the text property work?

    Peter

  6. 6 SB Apr 30th, 2008 at 2:15 am

    I am trying to style a TextArea that is called by a Repeater and I keep getting an error that Flex cannot access a property or method of a null object reference. I think it is because the TextArea doesn’t exist until after the stylesheet is called. Any ideas?

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".