Reputation: 1067
I'm creating the Label component like this
var label:Label = new Label();
label.text = "some text";
label.styleName = "someStyle";
addChild(label);
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?
Upvotes: 1
Views: 6824
Reputation: 31
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;
Upvotes: 3
Reputation: 1067
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;
Upvotes: 4
Reputation: 8050
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)
.
Upvotes: 1