views:

1732

answers:

2

Once I've set either the width or percentWidth property on a flex label, is there a way to reset the width to its default (i.e., the width of the text plus padding)?

I'm using the label as a renderer. In some cases, I'd like it to automatically size to the text, and in other cases, I'd like it to be a percentage width of its container. Obviously, I could use two separate labels, one for each of the above cases, but I'm curious if it's possible to reset the label to its default behavior.

+1  A: 

Set the width to undefined:

yourLabelId.width = undefined;

Stefan
Since width isn't typed as *, undefined will actually get converted to null. It might actually end up being NaN by the time your new height gets passed to where it is stored (explicitWidth, I believe).
joshtynjala
Setting my width to undefined seemed to work, but I agree that NaN is cleaner (I'm slowly learning the ActionScript type system). If this answer gets edited (s/undefined/NaN/g), I'll leave it "accepted," since it got me up and running quickly.
CapnNefarious
As a side note, it's worth mentioning that if I've set the percentWidth, then setting the width to NaN has no effect. The property I modified originally has to be set to NaN to reset the label to its default (measured) width. That's probably obvious, but potentially confusing.
CapnNefarious
+2  A: 
label.width = NaN;
//not sure if the following lines are required
label.invalidateSize();
label.width = label.measuredWidth;

From flex livedocs

When you set a specific height and width of a component, Flex does not call the measure() method, even if you explicitly call the invalidateSize() method. That is, Flex only calls the measure() method if the explicitWidth property or the explicitHeight property of the component is NaN.

Amarghosh
This worked for me, without needing the invalidateSize call or the assignment to measuredWidth. I'd seen that quote from the docs, but I didn't yet (and still probably don't) understand the relationship between the width and/or percentWidth properties and explicitWidth.
CapnNefarious
My understanding is that `explicitWidth` is the width of the visible part of the component. Scroll bars will appear if the actual width is more than this value. It is the explicitly specified width (by the programmer) of a component. If you set width, the same value is assigned to the explicitWidth property and if you specify percentWidth, the calculated width (based on parent's size) is used as explicitWidth.
Amarghosh