The following example shows how you can set the font sharpness on a Flex Label control by setting the fontSharpness and fontAntiAliasType styles.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/21/setting-the-font-sharpness-for-a-label-control-in-flex/ -->
<mx:Application name="Label_fontSharpness_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        @font-face {
            src: local("Arial");
            fontFamily: ArialEmbedded;
        }

        @font-face {
            src: local("Base 02");
            fontFamily: Base02Embedded;
        }
    </mx:Style>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="fontFamily:">
                <mx:ComboBox id="comboBox"
                        dataProvider="[ArialEmbedded,Base02Embedded]" />
            </mx:FormItem>
            <mx:FormItem label="fontSharpness:" direction="horizontal">
                <mx:HSlider id="slider"
                        minimum="-400"
                        maximum="400"
                        value="0"
                        snapInterval="10"
                        tickInterval="50"
                        liveDragging="true" />
                <mx:Label text="{slider.value}" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:Label id="lbl"
            text="The quick brown fox jumped over the lazy dog."
            fontFamily="{comboBox.selectedItem}"
            fontSize="16"
            fontAntiAliasType="advanced"
            fontSharpness="{slider.value}" />

</mx:Application>

View source is enabled in the following example.

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/21/setting-the-font-sharpness-for-a-label-control-in-flex/ -->
<mx:Application name="Label_fontSharpness_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        @font-face {
            src: local("Base 02");
            fontFamily: Base02Embedded;
        }

        Label {
            fontAntiAliasType: advanced;
            fontFamily: Base02Embedded;
            fontSize: 16;
            fontSharpness: 400;
        }
    </mx:Style>

    <mx:Label id="lbl"
            text="The quick brown fox jumped over the lazy dog." />

</mx:Application>

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/21/setting-the-font-sharpness-for-a-label-control-in-flex/ -->
<mx:Application name="Label_fontSharpness_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        @font-face {
            src: local("Arial");
            fontFamily: ArialEmbedded;
        }

        @font-face {
            src: local("Base 02");
            fontFamily: Base02Embedded;
        }

        .myLabel {
            fontAntiAliasType: advanced;
            fontFamily: ArialEmbedded;
            fontSize: 16;
        }
    </mx:Style>

    <mx:Script>
        <![CDATA[
            import mx.events.ListEvent;
            import mx.events.SliderEvent;

            private function comboBox_change(evt:ListEvent):void {
                lbl.setStyle("fontFamily", comboBox.selectedItem);
            }

            private function slider_change(evt:SliderEvent):void {
                lbl.setStyle("fontSharpness", evt.value);
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="fontFamily:">
                <mx:ComboBox id="comboBox"
                        dataProvider="[ArialEmbedded,Base02Embedded]"
                        change="comboBox_change(event);" />
            </mx:FormItem>
            <mx:FormItem label="fontSharpness:" direction="horizontal">
                <mx:HSlider id="slider"
                        minimum="-400"
                        maximum="400"
                        value="0"
                        snapInterval="10"
                        tickInterval="50"
                        liveDragging="true"
                        change="slider_change(event);" />
                <mx:Label text="{slider.value}" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:Label id="lbl"
            text="The quick brown fox jumped over the lazy dog."
            styleName="myLabel" />

</mx:Application>

Base 02 font by www.stereo-type.net.

 
Tagged with:
 
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.

4 Responses to Setting the font sharpness for a Label control in Flex

  1. ramya says:

    M getting these 2 errors:
    1)exception during transcoding: Font for alias ‘Base02Embedded’ with plain weight and style was not found by family name ‘Base 02′ fontsharpness/src fontsharpness.mxml line 16 1195721475530 2428
    2)unable to build font ‘Base02Embedded’ fontsharpness/src fontsharpness.mxml line 16 1195721475530 2429
    here font sharpness.mxml is the my application,how to reslove it,wat is Base 02′?

  2. ramya says:

    pls reply me

  3. peterd says:

    ramya,

    To run the example locally, you need to install the “Base 02″ font from http://www.stereo-type.net or use a different font (like Arial).

    Peter

  4. Gilles says:

    Does anyone know how you can get non-anti-aliased (bitmap) fonts properly embedded in Flex?

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