View Full Version : [newbie] A Label with width matching text
astgtciv
08-02-2006, 07:47 PM
How do you create an mx.controls.Label whose width would automatically
adjust when its .text is reset? Or, at least, how do you figure out which width to set the Label to once its .text is reset? The only thing I came up so far is "label.textWidth + 12" :) Thanks!
flexy
08-02-2006, 08:41 PM
As far as I can see, you don't need to specify a width for the Label component. It should simply expand or contract to accomodate this string you bind to it.
astgtciv
08-02-2006, 08:45 PM
Ah, perhaps the problem is that I am trying to set it programmatically by setting label.text=xxx instead of binding it...
flexy
08-02-2006, 09:03 PM
Try:
<mx:Script>
<![CDATA[
[Bindable] private var myText:String = "your text";
]]>
</mx:Script>
<mx:Label text="{myText}"/>
astgtciv
08-02-2006, 09:39 PM
Thank you, flexy. Your example definitely works. My problem wasn't the binding, though - it was that I was adding my label (via new Label(), addChild()) in a class that was extending mx.core.Container. Once I changed for that class to extend mx.containers.Canvas and added the label the same way, everything worked. I guess it's the container that is supposed to take care of the sizing of the components inside of it, and mx.core.Container simply does nothing, so I had to do it manually somehow. But mx.containers.Canvas does the sizing and layout automatically... I better go read about the component architecture in Flex. For some reason I am finding it very alien, coming from Flash...
flexy
08-03-2006, 06:55 PM
Ah yes, you need to extend a Container class, such as canvas, that already controls sizing and layout of children, otherwise you get the scenario you experienced. One of those things, you'll do it once, but never again!! :D
Good luck with the 'reading-up'; Flex documentation is sh1te at the moment! :mad:
astgtciv
08-03-2006, 07:23 PM
Do you expect the docs to be updated for the better? This definitely hasn't been the overall trend with MM over the years :D
One thing I can say is that the Flex docs certainly are very thorough... all the 2000 pdf pages of them. :)
vBulletin® v3.7.1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.