0 Replies Latest reply on Jul 12, 2010 9:39 AM by robin lankes

    Calculate pixel width of custom Actionscript component

    robin lankes

      I'm still relatively new to Flash Builder and Flex, but I didn't think that this particular situation would have me stumped like this.

       

      I've created a custom menu.mxml component and in there I'm reading a bunch of menu items out of an external xml file and then creating labels in ActionScript in a "for" loop. I've done this before, but in this case the Labels need to be Right-Aligned along a curve. I don't want to hard code the x-positions, since changing anything as simple as the font size would throw things out. So I want to take the x value of a point on the curve and then subtract the width of the label. Simple enough, but the darn width of the label always returns 0. I assume this has something to do with the label not yet having an actual width straight after creating it in ActionScript?

       

      I though I was onto something by overiding updateDisplayList() and reading the label width in there, but still getting 0 as width.

       

      Here's the function that draws the menu (sitting inside CDATA) - it's triggered by the creationComplete event:

       

      private function show():void

      {

           var numButtons:int;

           var menuItem:SolLabel;

           var mGroup:Group = new Group();

       

           var xpos:Array = new Array(410, 468, 475, 492, 411,355, 383, 374, 328, 329, 307, 352, 385);

       

           menuItems = new Array();

           numButtons = mAC.length;

           for (var i:uint = 0; i < numButtons; i++)

           {

                menuItem = new SolLabel();

                menuItem.text = mAC.getItemAt(i).name;

                menuItem.setStyle("color", 0x666666);

                menuItem.setStyle("fontSize", "16");

                menuItem.x = xpos[i] - menuItem.width;

                menuItem.y = menuSpacing * i;

                mGroup.addElement(menuItem);

                menuItems.push(menuItem);

           }

           addElement(mGroup);

      }

       

      SolLabel is just a mxml label component that uses the default font defined in the main CSS (so that I don't have to embed more fonts in ActionScript).

       

      I suppose menuItem.width (in pixels) doesn't have a value yet, but at what point does it have a value so that I can position the label before it is drawn on screen?