Using embedded fonts with the ComboBox control in Flex

by Peter deHaan on July 6, 2008

in ComboBox

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 comments… read them below or add one }

1 Anonymous July 10, 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!!!

Reply

2 Photoshop July 13, 2008 at 3:40 am

did not understand how fonts installed?

Reply

3 Frank July 31, 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

Reply

4 peterd July 31, 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

Reply

5 Frank July 31, 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

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: