I recently had a battle figuring out how to change the visual styles of the Flex UITextField component. It came highly recommended by many over the TextField in that it added among other things, the ability to “use styles”. They must mean it’s ability to inherit styles from its parent, because my experience with styling these monsters was a nightmare. Although now I know the trick, it won’t be nearly so hard next time.
My goal: to change the color and decoration of a UITextField when the data it displayed was a particular value.
Initial unsuccessful attempts:
Call to setStyle() in the updateDisplaylist() method.
Call setStyle() in the commitProperties() method.
Call getStyle() in the updateDisplaylist() method.
Call getStyle() in the commitProperties() method.
Call textColor = 0x0000FF; in both of the aforementioned methods.
During this first round, the only thing I tried that did work was to change the UITextField to a Label. Unfortunately that broke my goal, I reverted to this several times over the course of several hours as I struggled with this.
I didn’t start making progress until I refined my searches to the UITextField, rather than the UIComponent which I assumed was to blame. I also had a peek at the code and discovered that the implementation of setStyle() in UITextField was intentionally left empty, design by contract be damned. This discovery further allowed me refine my searches and I discovered the existence of the setTextFormat() method.
I got off to a rocky start with setTextFormat(), trying initially to call it from the createChildren() method. This didn’t work. I tried it in several places and eventually found that it works when it is placed in the commitProperties() method. I’ll have to figure out why sometime when I have more time.
Here’s the code that worked, inside commitProperties(). Keep in mind this is a list item renderer.
var formatter:TextFormat = new TextFormat(”Arial”, 11, 0×0000ff, false, false, true, null, null, “right”, 0,0,0,0);
var defTextFormat:TextFormat = new TextFormat(”Arial”, 11, 0×000000, false, false, false, null, null, “right”, 0,0,0,0);
_status.text = data.status;
if (data.status == “Changed”)
_status.setTextFormat(formatter);
else
_status.setTextFormat(defTextFormat);
You can lookup the TextFormat constructor in the documentation to see what all those fields mean, but they’re essentially most of the common styles you may want to use in a TextField. I may do some more experimentation/research to see if there’s a different/better place to put it (especially if you don’t want it being set/created each time the item renderer re-renders.