views:

167

answers:

2

Hi,

I want to create a button which resize dynamically with content. to achieve this, I created a MovieClip in library and added four layers into it namely - text, bg, shadow and border.

Problem I'm having is, if I make textfield autosize, only textfield resizes and others stuff remain as it is. if I calculate width required using xxxLineMetrics function and apply it to Button, background resizes properly but textfield also stretches with them and looks ugly.

I want backgrounds(sibling of textfield) resize properly with textfield so button looks nice with resized background and normal autosized textfield.

I hope u guys got what I want...any help appreciated...

Thanks,

A: 

you should apply the calculated width to the bg movieclip only and not to the whole movieclip. Because this will change the textfield's width too.

so try something like this:

txt.autoSize = TextFieldAutoSize.LEFT;
txt.text = "label";
btn.bg.width = txt.width;
yens resmann
I've tried this but it also stretch textfield!
Bhavesh.Bagadiya
and your textfiels isn't a child of your background movieclip?
yens resmann
My TExtFields and Background Sprite/Image both are siblings - child of a parent Sprite.
Bhavesh.Bagadiya
A: 

you can try the code below:

package  
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;

    /**
     * class represents the MyButton Movieclip in your library containing a dynamic 
     * textfield with instancename txt 
     * and a background movieclip with instance name bg
     */
    public class MyButton extends MovieClip
    {
     public var txt : TextField;
     public var bg : MovieClip;

     public function MyButton(label : String) 
     {
      txt.autoSize = TextFieldAutoSize.LEFT;
      txt.text = label;
      bg.width = txt.width;
     }

    }
}
yens resmann