Using embedded fonts with the Flash CS3 DataGrid component

A reader asked me the other day if it was possible to embed fonts into a Flash CS3 DataGrid control’s HeaderRenderer in an ActionScript 3.0 project.

After a bit of code shuffling, it seems it is possible. The trick was I had to create a HeaderRenderer instance and set the embedFonts style to true. Next, I set the row renderer’s embedFonts style to true and the textFormat style using the setRendererStyle() method, and finally set the headerTextFormat style using the setStyle() method.

Full code after the jump.

import fl.controls.dataGridClasses.DataGridColumn;import fl.controls.dataGridClasses.HeaderRenderer;import fl.data.DataProvider;var f1:Font = new VerdanaEmbedded() as Font;var headerTF:TextFormat = new TextFormat();headerTF.bold = false;headerTF.font = f1.fontName; // VerdanaheaderTF.size = 16;var rowTF:TextFormat = new TextFormat();rowTF.bold = false;rowTF.font = f1.fontName; // VerdanarowTF.size = 9;var fontArr:Array = Font.enumerateFonts(true);fontArr.sortOn("fontName", Array.CASEINSENSITIVE);var headRen:HeaderRenderer = new HeaderRenderer();headRen.setStyle("embedFonts", true);var fontNameCol:DataGridColumn = new DataGridColumn("fontName");fontNameCol.headerText = "fontName:";fontNameCol.headerRenderer = headRen;dataGrid.setRendererStyle("embedFonts", true);dataGrid.setRendererStyle("textFormat", rowTF);dataGrid.setStyle("headerTextFormat", headerTF);dataGrid.columns = [fontNameCol];dataGrid.dataProvider = new DataProvider(fontArr);dataGrid.rotation = 10;dataGrid.rowCount = 10;

Download FLA

Not sure if it is the “best” way, but it was the first solution I came up with. Any other tips/suggestions? Leave them in the comments!