Monday, July 24, 2006

Showing list of fonts in a Flash V2 comboBox formatted with the corresponding fonts

I was browsing the actionscript.org forums today and found a query by a user. It read "I have a combobox full of font names. I was thinking that it would be pretty cool to have the actual font format show in the label as well. ...."

I had a lil free time and thought I'd give it a shot.

This is what you need to do:

1. Drag a comboBox component from the components Panel to your FLA's Library.

2. Add the following code:


/* --------------------------- code starts here --------------------------- */
import mx.controls.ComboBox;
var myCB:ComboBox;

function init ()
{
myCB = ComboBox(createClassObject(ComboBox,"myCB", this.getNextHighestDepth()));
myCB.setSize(175,22);
myCB.rowCount = 7;
myCB.dataProvider = TextField.getFontList ();
myCB.open ();
myCB.addEventListener ("change", function ()
{
myCB.textField.setStyle ("fontFamily", myCB.value);
});
myCB.addEventListener ("load", setCBListFonts);
myCB.addEventListener ("scroll", setCBListFonts);
}
function setCBListFonts ()
{
for (var i in myCB.dropdown.listContent)
{
var _tf:TextFormat = myCB.dropdown.listContent[i].cell.getTextFormat ();
_tf.font = myCB.dropdown.listContent[i].cell.text;
myCB.dropdown.listContent[i].cell.setTextFormat (_tf);
}
}

init();
/* --------------------------- code ends here --------------------------- */


3. Test Movie, and you should be done!

This is how the final SWF looks: (If SWF doesnt show up below,check it out here
)



Post a Comment