
How to Format text in ComboBox and Set the Row Height in ActionScript
October 10, 2020ComboBox is one of the most frequently used component. When I added it into a flash movie via actionscript, I found I can control the format of the text displayed within the box it self, but the textFormat has no effect on the text in the comboBox. Here is what I do.
- var dpDownMenu:ComboBox = new ComboBox();
- dpDownMenu.width = 100;
- dpDownMenu.height = 22;
- dpDownMenu.x = 2.5;
- dpDownMenu.y = 4.0;
- dpDownMenu.textField.setStyle(”textFormat”, menuTextFormat);
- dpDownMenu.dataProvider = dropData;
- dpDownMenu.rowCount = uint(dropData.length);
- dpDownMenu.addEventListener(Event.CHANGE, adjustItems);
- addChild(dpDownMenu);
I try to format text (the labels) inside ComboBox component instance. The method setStyle seems to do very little to ComboBox components that it actually should do. I asked google and some experienced, in fact, the StyleManager.setStyle sets a style globally, i.e. for all components that are capable of using it. Although this is a way to force the ComboBox to format it’s text, its drawback is that you format nearly everything even if you don’t want to.
Not sure but, anyway the fact that StyleManager.setStyle works could be suggestion of what is actually happening. Internally the ComboBox component makes use of the List component. The List component in turn makes use of some CellRenderer class, that is responsible for rendering the separate cells.
It could be that when we set the textFormat style of ComboBox it ignores it instead of pushing it down to List and through List to CellRenderer. This could explain why the formatting doesn’t work with ComboBox.setStyle (for just one instance of ComboBox) nor it does with StyleManager.setComponentStyle (for the whole ComboBox class, but not other component classes).
On the other hand, when we use StyleManager.setStyle, we set the style for everything in the components structure, including the CellRenderer itself – and this is why in this single case it works.
The following source code works fine.
- var dpDownMenu:ComboBox = new ComboBox();
- dpDownMenu.width = 100;
- dpDownMenu.height = 22;
- dpDownMenu.addItem({data:1,label:’TestItem01′});
- dpDownMenu.addItem({data:2,label:’TestItem02′});
- dpDownMenu.addItem({data:3,label:’TestItem03′});
- dpDownMenu.x = 2.5;
- dpDownMenu.y = 4.0;
- var menuTextFormat:TextFormat = new TextFormat();
- menuTextFormat.size = 32;
- menuTextFormat.font = “Arial”;
- dpDownMenu.textField.setStyle(”textFormat”, menuTextFormat);
- dpDownMenu.dropdown.setRendererStyle(”textFormat”, menuTextFormat);
- dpDownMenu.dropdown.rowHeight = 22;
- dpDownMenu.dropdownWidth = 100;
- dpDownMenu.dataProvider = dropData;
- dpDownMenu.rowCount = uint(dropData.length);
- dpDownMenu.addEventListener(Event.CHANGE, adjustItems);
- addChild(dpDownMenu);