PDA

View Full Version : [AS3] can't set width on label


DrElvisToad
09-19-2008, 05:08 PM
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:

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!

rawmantick
09-19-2008, 05:42 PM
fl.controls.Label (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/controls/Label.html) 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 (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/controls/Label.html#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):
label.textField.height = label.textField.textHeight + 4;

Hope it helps

DrElvisToad
09-19-2008, 08:41 PM
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:

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

DrElvisToad
09-19-2008, 09:03 PM
nevermind, I got it to work:

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!