Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 09-19-2008, 04:08 PM   #1
DrElvisToad
Member
 
Join Date: Jun 2006
Location: Georgetown, Washington DC
Posts: 92
Thumbs up [AS3] can't set width on label

I'm trying to explicitly set the width of a label so that it stretches across the entire stage and has a dark background. No matter what I do, I just can't get the width to set. It always remains the same as the width of the text.

Here's the code:

ActionScript Code:
package  {     import flash.events.*;     import flash.display.Sprite;      import fl.controls.Label;     import flash.text.TextField;     import flash.text.TextFieldAutoSize;     import flash.display.Stage;     import flash.text.TextFormat;             public class Overlay extends Label {                 public function Overlay(){             super();             addEventListener(Event.ADDED_TO_STAGE, onAdded);             autoSize = TextFieldAutoSize.LEFT;             var format:TextFormat = new TextFormat();                     format.font = "Arial";                     format.color = 0xFFFFFF;                     format.size = 12;                     format.underline = false;             setStyle("textFormat", format);         }         //using ADDED_TO_STAGE so I can get a valid reference for the stage's width         private function onAdded(evt:Event){             width = stage.stageWidth;             height = textField.textHeight+10;                         trace(width + " "+   stage.stageWidth ); // traces "550 550" but the label still appears only as wide as its text                                 }     } }

even if I try setting the width to a hard-coded number like 5000 it still remains only as wide as the text. anyone encountered this? is there a fix? thanks!
__________________
--
Nerdabilly.com - 3.0 Like ActionScript, 2.0 Like the Web
DrElvisToad is offline   Reply With Quote
Old 09-19-2008, 04:42 PM   #2
rawmantick
Pipty rupees only!
 
rawmantick's Avatar
 
Join Date: Jul 2007
Location: Germany
Posts: 1,730
Default

fl.controls.Label class does not extend TextField. It extends UIComponent. So width and height properties actually dont mean what you think they do.

Instead you can access textField property of the Label, and try to set its width and height properties to what you need.

And by the way, to measure the height - it's enough to add only 4 pixels (constant for any text):
ActionScript Code:
label.textField.height = label.textField.textHeight + 4;

Hope it helps
__________________
Bang!
rawmantick is offline   Reply With Quote
Old 09-19-2008, 07:41 PM   #3
DrElvisToad
Member
 
Join Date: Jun 2006
Location: Georgetown, Washington DC
Posts: 92
Default

Hi, thanks for your help but it still isn't working.

I tried it with a class extending TextField also and it didn't work.

here's the new code:

ActionScript Code:
package  {     import flash.events.*;     import flash.display.Sprite;      import fl.controls.Label;     import flash.text.TextField;     import flash.text.TextFieldAutoSize;     import flash.display.Stage;     import flash.text.TextFormat;             public class Overlay extends Label {                                 public function Overlay(){             super();             addEventListener(Event.ADDED_TO_STAGE, onAdded);             autoSize = TextFieldAutoSize.LEFT;             var format:TextFormat = new TextFormat();             format.font = "Arial";             format.color = 0xFFFFFF;             format.size = 12;             format.underline = false;             textField.defaultTextFormat = format;             textField.backgroundColor = 0x000000;         }                 private function onAdded(evt:Event){             textField.width = stage.stageWidth;             textField.height += 4;                         trace(textField.width + " "+   stage.stageWidth ); // traces "280 550" but the label still appears only as wide as its text                                 }     } }

I'm just accessing the label component's textField, right? the textField width traces 280 even though I've set it to 550 with stage.stageWidth

Thanks
__________________
--
Nerdabilly.com - 3.0 Like ActionScript, 2.0 Like the Web
DrElvisToad is offline   Reply With Quote
Old 09-19-2008, 08:03 PM   #4
DrElvisToad
Member
 
Join Date: Jun 2006
Location: Georgetown, Washington DC
Posts: 92
Default

nevermind, I got it to work:

ActionScript Code:
package  {     import flash.text.TextFormatAlign;     import flash.events.Event;     import flash.text.TextField;     import flash.text.TextFieldAutoSize;     import flash.display.Stage;     import flash.text.TextFormat;             public class Overlay extends TextField {         public function Overlay(){             super();             addEventListener(Event.ADDED_TO_STAGE, onAdded);             var format:TextFormat = new TextFormat();             format.font = "Arial";             format.color = 0xFFFFFF;             format.size = 12;             format.underline = false;             format.align = TextFormatAlign.CENTER;             defaultTextFormat = format;             textColor = 0xFFffff;             background = true;             backgroundColor = 0x000000;         }                 private function onAdded(evt:Event){             width = stage.stageWidth;             height = textHeight + 4;         }     } }

I just ended up using TextField entirely.
Thanks!
__________________
--
Nerdabilly.com - 3.0 Like ActionScript, 2.0 Like the Web
DrElvisToad is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to reinitialize a mc - removeChild then addChild again? wu-tang ActionScript 3.0 1 08-30-2008 01:08 AM
Graphic Clipping/Stage Width FrancisLillie ActionScript 2.0 10 08-06-2007 12:53 PM
How to dynamically load MovieClip, create multiple instances, and set text label? notsofast ActionScript 3.0 0 08-03-2007 04:05 PM
Checkbox Component - label width problem nburlington Components 1 07-20-2007 01:11 PM
Getting a movie clip's width aaronkarp ActionScript 2.0 6 06-08-2007 04:47 AM


All times are GMT. The time now is 04:29 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.