Using embedded fonts in a Flex DataGrid control

by Peter deHaan on February 12, 2008

in DataGrid

The following example shows how you can use embedded fonts with the DataGrid control in Flex.

Note that the DataGridColumn’s use a bold font by default, so you’ll either need to either embed both a normal and bold font weight, or else set the DataGrid control’s column font weight to normal.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/02/12/using-embedded-fonts-in-a-flex-datagrid-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

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

        @font-face {
            src: local("Verdana");
            fontFamily: VerdanaEmbedded;
            fontWeight: bold;
        }
    </mx:Style>

    <mx:Script>
        <![CDATA[
            private var fontArr:Array;

            private function init():void {
                fontArr = Font.enumerateFonts(true);
                fontArr.sortOn("fontName", Array.CASEINSENSITIVE);
                dataGrid.dataProvider = fontArr;
            }
        ]]>
    </mx:Script>

    <mx:DataGrid id="dataGrid"
            fontFamily="VerdanaEmbedded"
            width="300"
            rowCount="10"
            creationComplete="init();"
            rotation="10">
        <mx:columns>
            <mx:DataGridColumn dataField="fontName"
                    headerText="fontName:" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

View source is enabled in the following example.

Alternately, you could just install the normal font face, and set a custom headerStyleName, as shown below:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/02/12/using-embedded-fonts-in-a-flex-datagrid-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

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

        .myHeaderStyleName {
            fontWeight: normal;
        }
    </mx:Style>

    <mx:Script>
        <![CDATA[
            private var fontArr:Array;

            private function init():void {
                fontArr = Font.enumerateFonts(true);
                fontArr.sortOn("fontName", Array.CASEINSENSITIVE);
                dataGrid.dataProvider = fontArr;
            }
        ]]>
    </mx:Script>

    <mx:DataGrid id="dataGrid"
            fontFamily="VerdanaEmbedded"
            headerStyleName="myHeaderStyleName"
            width="300"
            rowCount="10"
            creationComplete="init();"
            rotation="10">
        <mx:columns>
            <mx:DataGridColumn dataField="fontName"
                    headerText="fontName:" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

{ 2 comments… read them below or add one }

1 peterd February 12, 2008 at 10:09 pm

For an example of how to do this in Flash CS3, see “Using embedded fonts with the Flash CS3 DataGrid component”.

Peter

Reply

2 sanka January 13, 2010 at 10:26 pm

nice blog.keep it up.follow this to get example flex with EJB 3.0
http://www.codexamples.blogspot.com
thnx
Sanka

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: