06
Jul
08

Using embedded fonts with the ComboBox control in Flex

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.


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


  1. 1 Anonymous Jul 10th, 2008 at 10:18 am

    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. 2 Photoshop Jul 13th, 2008 at 3:40 am

    did not understand how fonts installed?

  3. 3 Frank Jul 31st, 2008 at 3:10 am

    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. 4 peterd Jul 31st, 2008 at 8:05 am

    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. 5 Frank Jul 31st, 2008 at 3:45 pm

    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

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".