27 January 2009 ~ 6 Comments

Using CSS to set properties in Flex

I am working on a project where I need to make the view layer fit into a small interface. Unlike in HTML, we can’t use css to control all of the look at feel features of the components in Flex. And example of something I wanted to do was to use CSS to change both the font-size and the height of a TextInput control via css. Unfortunately height is a property not a style, so be default it can not be addressed via CSS.

This is not a problem you can’t overcome, and although until yesterday I had never given this any consideration (thanks to Matt Legrand for putting me on the path). But all styles are defined inside the Actionscript metadata of our classes. So we can certainly add new styles to our favorite controls by extending them. An example of that can be seen below for the TextInput control.

package com.pnwrain.components
{
	import mx.controls.TextInput;

	[Style(name="height", type="Number", format="Length", inherit="no")]
	public class TextInput extends mx.controls.TextInput
	{
		public function TextInput()
		{
			super();
		}
		override public function get height():Number {
			return getStyle("height");
		}
	}
}

With this I can now use the Type selector CSS to control the height of my TextInput control.

TextInput {
	font-size:8;
	height: 15;
}

I am sure there are other ways to make this happen but this was a very short fix to a problem that could have been a little tricky. If you wanted to fool proof this you might check in the getter for height that the css style is actually set and default to something else, but this is just a simple sample of something I didn’t realize could happen.

HTH,
sim

Tags: , ,

6 Responses to “Using CSS to set properties in Flex”

  1. Dan 27 January 2009 at 11:51 pm Permalink

    I had a problem with your method in that the parent containers width and height were not getting updated correctly, thus causing scroll bars on the parent container.

    This guy goes about this in a different way which fixed my issues.

    http://www.craftymind.com/2008/03/31/hacking-width-and-height-properties-into-flexs-css-model/

    Thanks for the post as I have always wondered about setting width and height through css.

    -Dan

  2. Simeon 28 January 2009 at 8:14 am Permalink

    Hey Dan,

    Thanks for the link. Yeah I know my method is much to simple to be useful with anything complex, especially containers. But I had not even considered that this was possible so this is just a quick demo to show that this kind of thing can be done.

    Thanks for sharing your thoughts.

    sim

  3. Maxim Porges 6 February 2009 at 9:43 pm Permalink

    Why in God’s name Adobe didn’t make size and position part of Flex’s CSS support boggles my mind. It’s so useful in HTML-based CSS, and would be just as useful in Flex.

    Maybe we’ll see this in Gumbo, perhaps?

    - max

  4. judah 11 February 2009 at 6:04 pm Permalink

    Hi Simeon,

    Michael and I wrote something like this as well, http://www.judahfrangipane.com/blog/?p=56.

    Judah

  5. Toby Ho 27 March 2009 at 11:29 am Permalink

    Awesome tip! Thanks, this will be plenty useful for me.

  6. Kingschnulli 20 December 2009 at 10:35 am Permalink

    I have build a little class that takes out the overriding of all the classes, you only need to override the top most container of you application and you are done. Check it out here:

    http://myflex.wordpress.com/2009/12/20/adding-width-height-x-and-y-into-the-css-model-of-flex/


Leave a Reply

PHVsPjxsaT48c3Ryb25nPndvb19hYm91dDwvc3Ryb25nPiAtIEhpISBNeSBuYW1lIGlzIFNpbWVvbiBCYXRlbWFuIGFuZCBJIGFtIGEgd2ViIGFwcGxpY2F0aW9uIGRldmVsb3BlciBzcGVjaWFsaXppbmcgaW4gdGhlIEFkb2JlIEZsYXNoIFBsYXRmb3JtLiAgSSBhbSBhbiBBZG9iZSBDb21tdW5pdHkgRXhwZXJ0IGFuZCBhbiBBZG9iZSBDZXJ0aWZpZWQgVHJhaW5lciBmb3IgRmxleCBhbmQgQUlSLiAgSSBhbSBhbHNvIHRoZSBQcmluY2lwbGUgSW5zdGlnYXRvciBmb3IgUE5XIFJhaW4gTExDIGFuIFJJQSBjb25zdWx0aW5nIGFuZCBtZW50b3JpbmcgY29tcGFueS48L2xpPjxsaT48c3Ryb25nPndvb19hZHNfcm90YXRlPC9zdHJvbmc+IC0gdHJ1ZTwvbGk+PGxpPjxzdHJvbmc+d29vX2FkX2ltYWdlXzE8L3N0cm9uZz4gLSBodHRwOi8vd3d3Lndvb3RoZW1lcy5jb20vYWRzL3dvb3RoZW1lcy0xMjV4MTI1LTEuZ2lmPC9saT48bGk+PHN0cm9uZz53b29fYWRfaW1hZ2VfMjwvc3Ryb25nPiAtIGh0dHA6Ly93d3cud29vdGhlbWVzLmNvbS9hZHMvd29vdGhlbWVzLTEyNXgxMjUtMi5naWY8L2xpPjxsaT48c3Ryb25nPndvb19hZF9pbWFnZV8zPC9zdHJvbmc+IC0gaHR0cDovL3d3dy53b290aGVtZX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