The following example shows how you can use an embedded font with the Flex ComboBox control by setting the fontFamily style and dropdownStyleName style.

Full code after the jump.

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

    <mx:Style>
        @font-face {
            src: local("Comic Sans MS");
            fontFamily: EmbeddedComicSansMS;
            fontWeight: normal;
        }

        @font-face {
            src: local("Comic Sans MS");
            fontFamily: EmbeddedComicSansMS;
            fontWeight: bold;
        }

        ComboBox {
            fontFamily: EmbeddedComicSansMS;
        }
    </mx:Style>

    <mx:Array id="arr">
        <mx:Object label="One" />
        <mx:Object label="Two" />
        <mx:Object label="Three" />
        <mx:Object label="Four" />
        <mx:Object label="Five" />
        <mx:Object label="Six" />
        <mx:Object label="Seven" />
        <mx:Object label="Eight" />
        <mx:Object label="Nine" />
        <mx:Object label="Ten" />
    </mx:Array>

    <mx:ComboBox id="comboBox"
            dataProvider="{arr}" />

</mx:Application>

If you want to set the font separately on the ComboBox control’s dropdown menu, you can set the ComboBox control’s dropdownStyleName style, as seen in the following example:

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

    <mx:Style>
        @font-face {
            src: local("Comic Sans MS");
            fontFamily: EmbeddedComicSansMS;
            fontWeight: normal;
        }

        @font-face {
            src: local("Comic Sans MS");
            fontFamily: EmbeddedComicSansMS;
            fontWeight: bold;
        }

        ComboBox {
            dropdownStyleName: comboBoxDropdownStyles;
        }

        .comboBoxDropdownStyles {
            fontFamily: EmbeddedComicSansMS;
            fontWeight: normal;
        }
    </mx:Style>

    <mx:Array id="arr">
        <mx:Object label="One" />
        <mx:Object label="Two" />
        <mx:Object label="Three" />
        <mx:Object label="Four" />
        <mx:Object label="Five" />
        <mx:Object label="Six" />
        <mx:Object label="Seven" />
        <mx:Object label="Eight" />
        <mx:Object label="Nine" />
        <mx:Object label="Ten" />
    </mx:Array>

    <mx:ComboBox id="comboBox"
            dataProvider="{arr}" />

</mx:Application>

For an example of using different fonts in a ComboBox control’s dropdown menu, see “Displaying different fonts in a dropdown menu on a ComboBox control in Flex”.

For another example of using embedded fonts with the ComboBox control, see “Using an embedded font with the ComboBox control in Flex”. Sigh.

 
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.

5 Responses to Using embedded fonts with the ComboBox control in Flex

  1. Anonymous says:

    Is it a way you can set the font on each item in the ComboBox? For example I want “One” to be New Times Romain, “Two” to be Comic Sans, etc.

    Thanks

    P.S. Your blog ROCKS, I visit everyday for insight on Flex!!!

  2. Photoshop says:

    did not understand how fonts installed?

  3. Frank says:

    Good question Anon… I’m also trying to know I could show different type of font style for each entry in a combobox.

    I’m still looking for a solution to this problem… I thought it wouldn’t be something difficult to do, but I was wrong :/

    I’ll post here if I find the way to do it.

    Frank

  4. peterd says:

    Anonymous / Frank,

    You could use a custom item renderer. For more information, see “Displaying different fonts in a dropdown menu on a ComboBox control in Flex”.

    Peter

  5. Frank says:

    Hi Peter,

    That’s really cool, I had found a solution in Flash v2 this afternoon http://arulprasad.blogspot.com/2006/07/showing-list-of-fonts-in-flash-v2.html

    But as I’m using Flex 3 I truly appreciate that you took the time to think about it, I was still far from it.

    Thanks for your help,

    Frank

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