Take the 2-minute tour ×

I'm creating the Label component like this

var label:Label = new Label();
label.text = "some text";
label.styleName = "someStyle";

But it stay invisible until I explicit set the width and height.
How can I make the label to be auto resized according to it's text?

share|improve this question
Resized how? Do you want it to wrap onto a second line? Keep everything on a single line? –  Jason Towne Mar 11 '11 at 20:02
Lets say a single line –  GetUsername Mar 11 '11 at 20:07

3 Answers 3

up vote 4 down vote accepted

I've found the answer to my question here
The solution is to call a measureText() function for the label

var lineMetrics:TextLineMetrics = label.measureText(label.text);
label.width = lineMetrics.width;
label.height = lineMetrics.height;
share|improve this answer
this doesn't work for Spark components... –  Dan Aug 7 '11 at 13:45
Works with my Spark labels. –  Mario May 9 '13 at 8:39

I've also noticed the above answer doesn't seem to work with spark components. This does however work for me.

label.width = label.measuredWidth; label.height = label.measuredHeight;

share|improve this answer

You should be able to use label.percentWidth = 100; to allow the label to automatically grow with the text. If you want it to stay on a single line, you'll also want to set the maxDisplayedLines = 1; property as well.

You may also want to use addElement(label) instead of addChild(label).

share|improve this answer
That doesn't work, label remains invisible. Besides that, how is setting the component's width to 100% make it auto resized? –  GetUsername Mar 12 '11 at 6:00
It doesn't auto resize it per se, but sets the label's width to the width of its container. It might help if we could see some more of your code. You might also want to try calling label.validateNow() after adding the label to the container. –  Jason Towne Mar 12 '11 at 6:09

Your Answer


