12-24-2012 10:32 AM
Hi, I have the simplest question to which I cannot find the answer to
When adding label
var label:Label = new Label(); label.text = "Hello World"; label.width = label.textWidth; addChild(label);
I know that the width = 0 since textWidth doesn't return the actual width of the text right after it have been set. However, if I simply addChild(label), the text is cut off since the label doesn't automatically resize itself... How would I go about auto sizing the label?
Merry Christmas Eve everyone!
12-24-2012 01:14 PM - edited 12-24-2012 01:52 PM
yes, it seems a slightly bizarr implementation - by default a label seems to have a width of somthing like 90 and since it will truncate your text, the textWidth it returns will never be more than this default width ...
Here is a way around that:
var label:Label = new Label(); // give it all the space you can, to let it do the initial layout without trunkation label.width = stage.stageWidth; label.text = "Hello World"; addChild(label); // force rendering label.validateNow(); // now shorten the label to what is really needed label.width = label.textWidth;
There might be a more elegant solution, but this seems to work.
The last line assumes that you can reasonably expect the label to be shorter than the availalble width.
I guess, something like this would be better - just to be safe ...
// now shorten the label to what is really needed or truncate to available width
label.width = Math.min(myLabel.textWidth, stage.stageWidth);
My app : Get set - Get up! Get ready for the snooze revolution.