GetUsername
GetUsername

Reputation: 1067

How to make Flex Spark Label to be auto resized?

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

Answers (3)

Tim T
Tim T

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

GetUsername
GetUsername

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

Jason Towne
Jason Towne

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

Related Questions