Flex Tip of the Day: inline styles not permitted on UITextField
(One in a series of random, daily, useful Flex tips.)
In Flash, when you want to draw raw text, you use a TextField. In Flex, when you want to draw text and apply CSS styles, you use a UITextField, which adds support for CSS as well as many other things.
Because you can set the styleName of the UITextField and reference CSS values on it, you might also be tempted to think that you can also call setStyle() on it to set an inline style. But no, you can’t. There’s a setStyle() implementation in UITextField that does exactly nothing. This is documented, but it’s easy to miss.
I’m not sure why this method is there. I had guessed it was because it needed to be there in order to completely implement some interface, but I can’t find an interface that UITextField implements that has a setStyle() method in it. Anyone know?
Depending on why you needed to set an inline style, you might be able to work around this by calling setStyle() on the CSSStyleDeclaration that is governing the UITextField, or if you were just trying to save a value for later acces, you might be able to store the value as a property on the UITextField itself.
Technorati Tags: Flex, Flex Tip of the Day



I'm working on Noteflight, an online music writing application that lets you create, view, print and hear music notation with professional quality, right in your web browser. Work on a score from any computer on the Internet, share it with other users, and embed it in your own pages. Noteflight is free for individual use. Email me at dcoletta at noteflight dot com.

Very strange indeed, I had a look myself and couldn’t find anything either.
Perhaps some components call setStyle() on a textfield that can either be a UITextField or a normal TextField?
That’s the only theory I can come up with, but weird nevertheless..
Looks strange. I resolved this problem through setting parent styles, that UITextField is inheriting.
This was a super annoying “feature.” You can also use a UITextFormat and call setTextFormat() on the UITextField.
It looks like the UITextField used through out the Flex Framework (TextArea, Button, etc…) sets the textField.styleName = this. Which sets the UITextField’s style object to the parent component, typically UIComponent of some sort. The UITextField then takes on any of the style object’s properties.