Using a CFF embedded font with an MX Accordion container in Flex 4

The following example shows how you can use a CFF embedded font with a MX Accordion container by setting the textFieldClass style on the MX Accordion container’s header to the mx.core.UIFTETextField class.

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/16/using-a-cff-embedded-font-with-a-halo-accordion-container-in-flex-4/ -->
<s:Application name="MX_Accordion_headerStyleName_textFieldClass_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";
 
        @font-face {
            src: url("C:/Windows/Fonts/Comic.ttf");
            fontFamily: ComicDF4;
            embedAsCFF: true;
        }
 
        .headerStylez {
            fontFamily: ComicDF4;
            fontSize: 14;
            textFieldClass: ClassReference("mx.core.UIFTETextField");
        }
    </fx:Style>
 
    <mx:Accordion id="accordion"
            headerStyleName="headerStylez"
            width="300" height="200"
            horizontalCenter="0" verticalCenter="0">
        <mx:VBox label="Label">
            <mx:Label text="MX Label" />
        </mx:VBox>
        <mx:VBox label="Text">
            <mx:Label text="MX Text" />
        </mx:VBox>
        <mx:VBox label="TextInput">
            <mx:TextInput text="MX TextInput" />
        </mx:VBox>
        <mx:VBox label="TextArea">
            <mx:TextArea text="MX TextArea" />
        </mx:VBox>
        <mx:VBox label="Button">
            <mx:Button label="MX Button" />
        </mx:VBox>
    </mx:Accordion>
 
</s:Application>

[GoogleAdsWide]

View source is enabled in the following example.

You can also set the headerStyleName 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/2009/08/16/using-a-cff-embedded-font-with-a-halo-accordion-container-in-flex-4/ -->
<s:Application name="MX_Accordion_headerStyleName_textFieldClass_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";
 
        @font-face {
            src: url("C:/Windows/Fonts/Comic.ttf");
            fontFamily: ComicDF4;
            embedAsCFF: true;
        }
 
        .headerStylez {
            fontFamily: ComicDF4;
            fontSize: 14;
            textFieldClass: ClassReference("mx.core.UIFTETextField");
        }
 
        mx|Accordion {
            headerStyleName: headerStylez;
        }
    </fx:Style>
 
    <mx:Accordion id="accordion"
            width="300" height="200"
            horizontalCenter="0" verticalCenter="0">
        <mx:VBox label="Label">
            <mx:Label text="MX Label" />
        </mx:VBox>
        <mx:VBox label="Text">
            <mx:Label text="MX Text" />
        </mx:VBox>
        <mx:VBox label="TextInput">
            <mx:TextInput text="MX TextInput" />
        </mx:VBox>
        <mx:VBox label="TextArea">
            <mx:TextArea text="MX TextArea" />
        </mx:VBox>
        <mx:VBox label="Button">
            <mx:Button label="MX Button" />
        </mx:VBox>
    </mx:Accordion>
 
</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/08/16/using-a-cff-embedded-font-with-a-halo-accordion-container-in-flex-4/ -->
<s:Application name="MX_Accordion_headerStyleName_textFieldClass_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";
 
        @font-face {
            src: url("C:/Windows/Fonts/Comic.ttf");
            fontFamily: ComicDF4;
            embedAsCFF: true;
        }
 
        .headerStylez {
            fontFamily: ComicDF4;
            fontSize: 14;
            textFieldClass: ClassReference("mx.core.UIFTETextField");
        }
    </fx:Style>
 
    <fx:Script>
        <![CDATA[
            protected function btn_click(evt:MouseEvent):void {
                accordion.setStyle("headerStyleName", "headerStylez");
            }
        ]]>
    </fx:Script>
 
    <s:Button id="btn"
            label="Set header style name"
            click="btn_click(event);"
            left="10" top="10" />
 
    <mx:Accordion id="accordion"
            width="300" height="200"
            horizontalCenter="0" verticalCenter="0">
        <mx:VBox label="Label">
            <mx:Label text="MX Label" />
        </mx:VBox>
        <mx:VBox label="Text">
            <mx:Label text="MX Text" />
        </mx:VBox>
        <mx:VBox label="TextInput">
            <mx:TextInput text="MX TextInput" />
        </mx:VBox>
        <mx:VBox label="TextArea">
            <mx:TextArea text="MX TextArea" />
        </mx:VBox>
        <mx:VBox label="Button">
            <mx:Button label="MX Button" />
        </mx:VBox>
    </mx:Accordion>
 
</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/16/using-a-cff-embedded-font-with-a-halo-accordion-container-in-flex-4/ -->
<s:Application name="MX_Accordion_headerStyleName_textFieldClass_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"
        initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import mx.containers.Accordion;
            import mx.containers.VBox;
            import mx.controls.Button;
            import mx.controls.Label;
            import mx.controls.Text;
            import mx.controls.TextArea;
            import mx.controls.TextInput;
            import mx.core.UIFTETextField;
 
            [Embed(source="C:/Windows/Fonts/Comic.ttf", fontFamily="ComicDF4", embedAsCFF="true")]
            private const ComicFont:Class;
 
            protected var accordion:Accordion;
 
            protected function init():void {
                var headerStylez:CSSStyleDeclaration = new CSSStyleDeclaration(".headerStylez");
                headerStylez.setStyle("fontFamily", "ComicDF4");
                headerStylez.setStyle("fontSize", 14);
                headerStylez.setStyle("textFieldClass", UIFTETextField);
 
                // child 1
                var lbl1:Label = new Label();
                lbl1.text = "MX Label";
 
                var vBox1:VBox = new VBox();
                vBox1.label = "Label";
                vBox1.addElement(lbl1);
 
                // child 2
                var txt1:Text = new Text();
                txt1.text = "MX Text";
 
                var vBox2:VBox = new VBox();
                vBox2.label = "Text";
                vBox2.addElement(txt1);
 
                // child 3
                var txtInput1:TextInput = new TextInput();
                txtInput1.text = "MX TextInput";
 
                var vBox3:VBox = new VBox();
                vBox3.label = "TextInput";
                vBox3.addElement(txtInput1);
 
                // child 4
                var txtArea1:TextArea = new TextArea();
                txtArea1.text = "MX TextArea";
 
                var vBox4:VBox = new VBox();
                vBox4.label = "TextArea";
                vBox4.addElement(txtArea1);
 
                // child 5
                var btn1:Button = new Button();
                btn1.label = "MX Button";
 
                var vBox5:VBox = new VBox();
                vBox5.label = "Button";
                vBox5.addElement(btn1);
 
                // Accordion
                accordion = new Accordion();
                accordion.setStyle("headerStyleName", "headerStylez");
                accordion.width = 300;
                accordion.height = 200;
                accordion.horizontalCenter = 0;
                accordion.verticalCenter = 0;
                accordion.addElement(vBox1);
                accordion.addElement(vBox2);
                accordion.addElement(vBox3);
                accordion.addElement(vBox4);
                accordion.addElement(vBox5);
                addElement(accordion);
            }
        ]]>
    </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.