Posted by: Yogesh Puri on: June 10, 2008
We have encountered a situation where the the UI button label was changing frequently in Flash CS3. So basis on that we have to resize our button component as the label changes. There is no inbuilt AutoResize feature in button component so what we have done is, get the size of the label and then set the width of the button. Following is the code we have implemented in this case. Put a UI button on stage and name it “_btn”. You can download the source files from here.
import flash.events.Event;
//———-
var t_arr:Array = ["This is string 1","Hello","Hello to String 3","Hello to String 4","Hello to String 5","This is a long string NOW "];
//——— Set the button textfield properties ———-
_btn.textField.autoSize = TextFieldAutoSize.LEFT; // We need to set the autosize to LEFT?RIGHT or CENTER instead of NONE
_btn.textField.multiline = false; // Make sure the multiline is false in this case to get proper dimensions
_btn.label = t_arr[0];
_btn.textField.addEventListener(Event.RENDER,resizeBtn); // Add an event listener to textfield on render event
_btn.addEventListener(MouseEvent.CLICK,clickHandler);
//——– Resize the button ———–
function resizeBtn(evt:Event):void{
// trace(evt.target.width+” “+evt.target.parent.width);
_btn.width = evt.target.width+5;
_btn.validateNow();
}
//———— Handle button click ———
function clickHandler(evt:MouseEvent):void{
_btn.label = t_arr[Math.floor(6*Math.random())];
}
Possibly related posts: (automatically generated)
Albeo theme by Design Disease
Recent Comments