Using a CFF embedded font with a Halo DataGrid control in Flex 4

The following example shows how you can use a CFF embedded font with a Halo DataGrid container by setting the itemRenderer property or defaultDataGridItemRenderer style to the mx.controls.dataGridClasses.TLFDataGridItemRenderer class.

Full code after the jump.

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/2009/08/21/using-a-cff-embedded-font-with-a-halo-datagrid-control-in-flex-4/ -->
<s:Application name="Halo_DataGrid_TLFDataGridItemRenderer_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">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/halo";
 
        @font-face {
            src: url("assets/Arial.ttf");
            fontFamily: ArialCFF;
            embedAsCFF: true;
        }
    </fx:Style>
 
    <s:VGroup horizontalCenter="0" verticalCenter="0">
        <mx:DataGrid id="dataGrid"
                itemRenderer="mx.controls.dataGridClasses.TLFDataGridItemRenderer"
                fontFamily="ArialCFF">
            <mx:columns>
                <mx:DataGridColumn dataField="c1" />
                <mx:DataGridColumn dataField="c2" />
            </mx:columns>
            <mx:dataProvider>
                <mx:ArrayList>
                    <fx:Object c1="1. One" c2="1. Two" />
                    <fx:Object c1="2. One" c2="2. Two" />
                    <fx:Object c1="3. One" c2="3. Two" />
                    <fx:Object c1="4. One" c2="4. Two" />
                    <fx:Object c1="5. One" c2="5. Two" />
                    <fx:Object c1="6. One" c2="6. Two" />
                    <fx:Object c1="7. One" c2="7. Two" />
                    <fx:Object c1="8. One" c2="8. Two" />
                    <fx:Object c1="9. One" c2="9. Two" />
                </mx:ArrayList>
            </mx:dataProvider>
        </mx:DataGrid>
        <s:SimpleText text="Spark SimpleText with embedded font" fontFamily="ArialCFF" />
    </s:VGroup>
 
</s:Application>

[GoogleAdsWide]

You can set the defaultDataGridItemRenderer style in MXML, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/08/21/using-a-cff-embedded-font-with-a-halo-datagrid-control-in-flex-4/ -->
<s:Application name="Halo_DataGrid_TLFDataGridItemRenderer_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">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/halo";
 
        @font-face {
            src: url("assets/Arial.ttf");
            fontFamily: ArialCFF;
            embedAsCFF: true;
        }
    </fx:Style>
 
    <s:VGroup horizontalCenter="0" verticalCenter="0">
        <mx:DataGrid id="dataGrid"
                defaultDataGridItemRenderer="mx.controls.dataGridClasses.TLFDataGridItemRenderer"
                fontFamily="ArialCFF">
            <mx:columns>
                <mx:DataGridColumn dataField="c1" />
                <mx:DataGridColumn dataField="c2" />
            </mx:columns>
            <mx:dataProvider>
                <mx:ArrayList>
                    <fx:Object c1="1. One" c2="1. Two" />
                    <fx:Object c1="2. One" c2="2. Two" />
                    <fx:Object c1="3. One" c2="3. Two" />
                    <fx:Object c1="4. One" c2="4. Two" />
                    <fx:Object c1="5. One" c2="5. Two" />
                    <fx:Object c1="6. One" c2="6. Two" />
                    <fx:Object c1="7. One" c2="7. Two" />
                    <fx:Object c1="8. One" c2="8. Two" />
                    <fx:Object c1="9. One" c2="9. Two" />
                </mx:ArrayList>
            </mx:dataProvider>
        </mx:DataGrid>
        <s:SimpleText text="Spark SimpleText with embedded font" fontFamily="ArialCFF" />
    </s:VGroup>
 
</s:Application>

Or, you also set the defaultDataGridItemRenderer style globally 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/2009/08/21/using-a-cff-embedded-font-with-a-halo-datagrid-control-in-flex-4/ -->
<s:Application name="Halo_DataGrid_TLFDataGridItemRenderer_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">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/halo";
 
        @font-face {
            src: url("assets/Arial.ttf");
            fontFamily: ArialCFF;
            embedAsCFF: true;
        }
 
        mx|DataGrid {
            defaultDataGridItemRenderer: ClassReference("mx.controls.dataGridClasses.TLFDataGridItemRenderer");
            fontFamily: ArialCFF;
        }
 
        s|SimpleText {
            fontFamily: ArialCFF;
        }
    </fx:Style>
 
    <s:VGroup horizontalCenter="0" verticalCenter="0">
        <mx:DataGrid id="dataGrid">
            <mx:columns>
                <mx:DataGridColumn dataField="c1" />
                <mx:DataGridColumn dataField="c2" />
            </mx:columns>
            <mx:dataProvider>
                <mx:ArrayList>
                    <fx:Object c1="1. One" c2="1. Two" />
                    <fx:Object c1="2. One" c2="2. Two" />
                    <fx:Object c1="3. One" c2="3. Two" />
                    <fx:Object c1="4. One" c2="4. Two" />
                    <fx:Object c1="5. One" c2="5. Two" />
                    <fx:Object c1="6. One" c2="6. Two" />
                    <fx:Object c1="7. One" c2="7. Two" />
                    <fx:Object c1="8. One" c2="8. Two" />
                    <fx:Object c1="9. One" c2="9. Two" />
                </mx:ArrayList>
            </mx:dataProvider>
        </mx:DataGrid>
        <s:SimpleText text="Spark SimpleText with embedded font" />
    </s:VGroup>
 
</s:Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/08/21/using-a-cff-embedded-font-with-a-halo-datagrid-control-in-flex-4/ -->
<s:Application name="Halo_DataGrid_TLFDataGridItemRenderer_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"
        initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayList;
            import mx.controls.DataGrid;
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.controls.dataGridClasses.TLFDataGridItemRenderer;
            import spark.components.VGroup;
            import spark.primitives.SimpleText;
 
            [Embed(source="assets/Arial.ttf",
                    fontFamily="ArialCFF",
                    embedAsCFF="true")]
            private const ArialEmbeddedFont:Class;
 
            private var dataGrid:DataGrid;
            private var simpleTxt:SimpleText;
 
            private function init():void {
                var dp:ArrayList = new ArrayList();
                dp.addItem({c1:"1. One", c2:"1. Two"});
                dp.addItem({c1:"2. One", c2:"2. Two"});
                dp.addItem({c1:"3. One", c2:"3. Two"});
                dp.addItem({c1:"4. One", c2:"4. Two"});
                dp.addItem({c1:"5. One", c2:"5. Two"});
                dp.addItem({c1:"6. One", c2:"6. Two"});
                dp.addItem({c1:"7. One", c2:"7. Two"});
                dp.addItem({c1:"8. One", c2:"8. Two"});
                dp.addItem({c1:"9. One", c2:"9. Two"});
 
                var cols:Array = [];
                cols.push(new DataGridColumn("c1"));
                cols.push(new DataGridColumn("c2"));
 
                dataGrid = new DataGrid();
                dataGrid.columns = cols;
                dataGrid.dataProvider = dp;
                dataGrid.itemRenderer = new ClassFactory(TLFDataGridItemRenderer);
                dataGrid.setStyle("fontFamily", "ArialCFF");
 
                simpleTxt = new SimpleText();
                simpleTxt.text = "Spark SimpleText with embedded font";
                simpleTxt.setStyle("fontFamily", "ArialCFF");
 
                var vGr:VGroup = new VGroup();
                vGr.horizontalCenter = 0;
                vGr.verticalCenter = 0;
                vGr.addElement(dataGrid);
                vGr.addElement(simpleTxt);
                addElement(vGr);
            }
        ]]>
    </fx:Script>
 
</s:Application>

Or, you can use the StyleManager class and set the defaultDataGridItemRenderer style globally, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/08/21/using-a-cff-embedded-font-with-a-halo-datagrid-control-in-flex-4/ -->
<s:Application name="Halo_DataGrid_TLFDataGridItemRenderer_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"
        initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayList;
            import mx.controls.DataGrid;
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.controls.dataGridClasses.TLFDataGridItemRenderer;
            import spark.components.VGroup;
            import spark.primitives.SimpleText;
 
            [Embed(source="assets/Arial.ttf",
                    fontFamily="ArialCFF",
                    embedAsCFF="true")]
            private const ArialEmbeddedFont:Class;
 
            private var dataGrid:DataGrid;
            private var simpleTxt:SimpleText;
 
            private function init():void {
                var dgStyles:CSSStyleDeclaration = StyleManager.getStyleDeclaration("mx.controls.DataGrid");
                if (!dgStyles) { /* null check */
                    dgStyles = new CSSStyleDeclaration("mx.controls.DataGrid");
                }
                dgStyles.setStyle("defaultDataGridItemRenderer", TLFDataGridItemRenderer);
                dgStyles.setStyle("fontFamily", "ArialCFF");
 
                var stStyles:CSSStyleDeclaration = StyleManager.getStyleDeclaration("spark.primitives.SimpleText");
                if (!stStyles) { /* null check */
                    stStyles = new CSSStyleDeclaration("spark.primitives.SimpleText");
                }
                stStyles.setStyle("fontFamily", "ArialCFF");
 
                var dp:ArrayList = new ArrayList();
                dp.addItem({c1:"1. One", c2:"1. Two"});
                dp.addItem({c1:"2. One", c2:"2. Two"});
                dp.addItem({c1:"3. One", c2:"3. Two"});
                dp.addItem({c1:"4. One", c2:"4. Two"});
                dp.addItem({c1:"5. One", c2:"5. Two"});
                dp.addItem({c1:"6. One", c2:"6. Two"});
                dp.addItem({c1:"7. One", c2:"7. Two"});
                dp.addItem({c1:"8. One", c2:"8. Two"});
                dp.addItem({c1:"9. One", c2:"9. Two"});
 
                var cols:Array = [];
                cols.push(new DataGridColumn("c1"));
                cols.push(new DataGridColumn("c2"));
 
                dataGrid = new DataGrid();
                dataGrid.columns = cols;
                dataGrid.dataProvider = dp;
 
                simpleTxt = new SimpleText();
                simpleTxt.text = "Spark SimpleText with embedded font";
 
                var vGr:VGroup = new VGroup();
                vGr.horizontalCenter = 0;
                vGr.verticalCenter = 0;
                vGr.addElement(dataGrid);
                vGr.addElement(simpleTxt);
                addElement(vGr);
            }
        ]]>
    </fx:Script>
 
</s:Application>

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.

3 thoughts on “Using a CFF embedded font with a Halo DataGrid control in Flex 4

Comments are closed.