Setting the line height on a Spark RichText control in Flex 4

The following example shows how you can set the line height on a Spark RichText control in Flex 4 by setting the lineHeight 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/03/18/setting-the-line-height-on-a-spark-richtext-control-in-flex-4/ -->
<s:Application name="Spark_RichText_lineHeight_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:controlBarContent>
        <mx:Form>
            <mx:FormItem label="lineHeight:">
                <s:HSlider id="sl" minimum="10" maximum="24" value="14" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <s:RichText id="richTxt"
            lineHeight="{sl.value}"
            textAlign="justify"
            left="20" right="20"
            top="20">
        <s:text><fx:String source="lorem.txt" /></s:text>
    </s:RichText>
 
</s:Application>

You can also set the lineHeight style in an external .CSS file or <Style> block, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/03/18/setting-the-line-height-on-a-spark-richtext-control-in-flex-4/ -->
<s:Application name="Spark_RichText_lineHeight_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:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";
 
        s|RichText {
            lineHeight: 24;
        }
    </fx:Style>
 
    <s:RichText id="richTxt"
            textAlign="justify"
            left="20" right="20"
            top="20">
        <s:text><fx:String source="lorem.txt" /></s:text>
    </s:RichText>
 
</s:Application>

Or you can set the lineHeight style using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/03/18/setting-the-line-height-on-a-spark-richtext-control-in-flex-4/ -->
<s:Application name="Spark_RichText_lineHeight_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:controlBarContent>
        <mx:Form>
            <mx:FormItem label="lineHeight:">
                <s:HSlider id="sl"
                        minimum="10" maximum="24"
                        value="14"
                        change="sl_changeHandler(event);" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            protected function sl_changeHandler(evt:Event):void {
                richTxt.setStyle("lineHeight", sl.value);
            }
        ]]>
    </fx:Script>
 
    <s:RichText id="richTxt"
            textAlign="justify"
            left="20" right="20"
            top="20">
        <s:text><fx:String source="lorem.txt" /></s:text>
    </s:RichText>
 
</s:Application>

You can also set the lineHeight style to a percentage value, as seen in the following snippets.
MXML:

<s:RichText id="richTxt"
        lineHeight="200%">
    <s:text><fx:String source="lorem.txt" /></s:text>
</s:RichText>

CSS:

<fx:Style>
    @namespace s "library://ns.adobe.com/flex/spark";
 
    s|RichText {
        lineHeight: "200%";
    }
</fx:Style>

ActionScript:

<fx:Script>
    <![CDATA[
        protected function hslider1_changeHandler(evt:Event):void {
            richTxt.setStyle("lineHeight", "200%");
        }
    ]]>
</fx:Script>

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.