Displaying different fonts in a dropdown menu on a ComboBox control in Flex
The following example shows how you can set different fonts for different items in a Flex ComboBox control’s dropdown menu by using a custom item renderer.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/07/31/displaying-different-fonts-in-a-dropdown-menu-on-a-combobox-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="top"
backgroundColor="white">
<mx:ArrayCollection id="arrColl"
source="{Font.enumerateFonts(true)}">
<mx:sort>
<mx:Sort>
<mx:fields>
<mx:SortField name="fontName" />
</mx:fields>
</mx:Sort>
</mx:sort>
</mx:ArrayCollection>
<mx:ComboBox id="comboBox"
dataProvider="{arrColl}"
labelField="fontName"
fontSize="14"
open="comboBox.dropdown.variableRowHeight = true;">
<mx:itemRenderer>
<mx:Component>
<mx:Label fontFamily="{data.fontName}"
toolTip="{data.fontName}" />
</mx:Component>
</mx:itemRenderer>
</mx:ComboBox>
</mx:Application>
View source is enabled in the following example.
For an example of doing this in the Flex 4 SDK, see “Displaying different fonts in a Spark List control in Flex 4″.
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.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
15 Responses to Displaying different fonts in a dropdown menu on a ComboBox control in Flex
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


I cannot seem to figure out how to pull the selected font to a textbox so I can see what is selected. I have tried all kinds of things:
{myFont.labelField}
{myFont.selectedItem.label}
The comboBox id is myFont
Any insight would be great
Thanks
G.
George,
<mx:Label text="{comboBox.selectedItem.fontName}" />Peter
Love the site, lots of good nuggets.
Building upon this topic, any ideas how to add an embedded font to a Rich Text Editor? Not just assign the embedded font, but append multiple to the RTE. I’ve always been able to push a
fontNameto thefontFamilyArray, but the font never renders in the RTE, I always get a default font to render.Example:
<mx:Style> @font-face { src:url("AABIGFICTION.swf"); fontFamily: "AABIGFICTION"; } </mx:Style> <mx:Script> RTE.fontFamilyArray.push('AABIGFICTION'); </mx:Script> <mx:RichTextEditor fontAntiAliasType="advanced" x="134" y="21" id="RTE" title="Title" width="440" height="368" enabled="true"></:RichTextEditor>Chris,
I don’t believe you can mix embedded fonts and non-embedded fonts in a text field (but of course, I could be wrong). I did a quick test and it seems my text was disappearing after selecting some text and changing the font family.
That said, if I only use embedded fonts in the RichTextEditor, then this code seems to work:
Of course, since the RichTextEditor control allows users to change the text to bold or italic (or both), you’d want to embed the regular font face as well as the combination of bold, italic, and bold+italic font faces (so embed four font faces per font family).
Hope that helps,
Peter
Peter,
You are right, you cant’ mix device and embedded fonts in a text field. If I embed a device font like you did in your example, I get the same result, it works perfectly.
But, if I use a compiled swf containing a whole fontfamily, or embed a true type or open type font, I can’t get the fonts to render right. I can assign a font like in the example below, but if I try to utilize the fontfamilyarray, you get a default font rendering. The underlying html though, shows the correct font selection, the rendering in the RTE only doesn’t show the right font.
I can get a font (AABIGFICTION) to work like this:
<mx:Style> @font-face { src:url("AABIGFICTION.swf"); fontFamily: "AABIGFICTION"; } @font-face { src:url("Baskerville_Old_Face.swf"); fontFamily: "Baskerville_Old_Face"; } </mx:Style> <mx:Script> <![CDATA[ private function returnHandler():void { RTE.fontFamilyArray = ["AABIGFICTION", "Baskerville_Old_Face"]; } ]]> </mx:Script> <mx:RichTextEditor id="RTE" title="Title" width="440" fontFamily="AABIGFICTION" height="368" creationComplete="returnHandler();">I just figured this one out. Make sure that if you embed a font, that every font used by the movie is embedded. Because if flex tries to use a font that is not embedded, Flex will automatically switch to using system fonts. IT WILL IGNORE YOUR EMBEDDED FONTS.
In my project, I was embedding 2 non-system fonts. The Rich Text editor by default uses Verdana to display your text size, font, all the control components that belong to the Rich Text Editor… And since I didn’t embed Verdana, Flex ignored my embedded fonts, and forced the movie to use Verdana. After I embedded the system font (Verdana) everything worked as expected.
do you know how to get a combo box in a rich text editor to do this. Trying to do it in AS3 not MXML.
Hi,
I would like to know how to create a ComboBox which contains and displays many embedded fonts in a dropdown menu with MXML ?
Thanks for you help.
My local machine available of some unicode font in chinese character. Once i try with this application inside the drop down list found that unicode font is render inproper appear as “???????”. Any way to resolve this?
dear sir
i have found code for fomt-family dropdown menu and you give this code in your website that i cant understand in which format should i save it doesn’t run save as html mhtml mxml actually i want to use them in my web page but i cant use it so please kindly tel me what type of file should i create with your code
gosai kishorgiri keshavgiri,
Here’s a quick “how to”:
1) Create a new Flex Project in Flex Builder 3.
2) Copy the code from the post into your new Flex project’s default .MXML file.
3) Run the project from Flex Builder and verify it works.
4) Select Project > Export Release Build from the Flex Builder main menu. This will export all the files to some .HTML, .JS, and a .SWF files.
5) Upload the files from the Flex Project’s /bin-release/ to your website.
6) View the .HTML file on your website.
Peter
That’s great code, but I am wondering and is there a way to achieve same effect with flash cs3 combobox control and actionscript 3
hi,
can you please give me the as3 code for flash to do the same !
thanks
Hi,
I have tried this code, but it run incorrectly on flash 10. Weddings, Winding and some simsun-PUA is display incorrectly.
Can you help me to figure out what ‘s wrong with it.
Here ‘s quick “how to”:
1) 1) Create a new Flex Project in Flex Builder 3.
2) Copy the code from the post into your new Flex project’s default .MXML file.
3) Open your project properties. On the Flex Compiler, input “10.0.10″ for the item “Require Flash Player version” to specify the flash version
3) Run the project from Flex Builder and verify it works.
Hi Peter
My question is i only want to show some specific embedded fonts in the dropdownlist not the system fonts
thanks in advance