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 thoughts on “Using embedded fonts with the ComboBox control in Flex

  1. 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. 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

Comments are closed.