Getting resize height value of Autosize label

Have a wee problem here:

I’ve got a fuction that’s looped and creates numerous labels - I want to get the height of each so I can place them neatly below one another. I’m using a listener to get the ‘dynamic height’ of the label created but am having problems returning the value to the addTextRP to use - any ideas?

 
  public function addTextRP(thisText:String, i:Number):void {
   var lblFld:Label = new Label();
   lblFld.x=0;
   lblFld.width = 160;
   lblFld.wordWrap=true;
   lblFld.autoSize = TextFieldAutoSize.CENTER;
   lblFld.y=txtHeight;
   lblFld.name = "txtFld" + i;
   lblFld.text=thisText;
   lblFld.addEventListener(ComponentEvent.RESIZE, resizeHandler);
   addChild(lblFld);
   var thisHeight:Number = txtFld.height; [COLOR=red]// doesn't get the right height[/COLOR]
}

 
  function resizeHandler(event:ComponentEvent):Number {
   var lbl:Label = event.currentTarget as Label;
   trace("width:" + lbl.width, "height:" + lbl.height);[COLOR=red] // this traces the correct heights[/COLOR]
   return(lbl.height);
  }

Any help would be fantastic!

Many thanks,

partimer

I’m a bit confused;


public function addTextRP(thisText:String, i:Number):void {
   var lblFld:Label = new Label();
   lblFld.x=0;
   lblFld.width = 160;
   lblFld.wordWrap=true;
   lblFld.autoSize = TextFieldAutoSize.CENTER;
   lblFld.y=txtHeight;
   lblFld.name = "txtFld" + i;
   lblFld.text=thisText;
   lblFld.addEventListener(ComponentEvent.RESIZE, resizeHandler);
   addChild(lblFld);
   var thisHeight:Number = txtFld.height; // doesn't get the right height
}

You say it’s not getting the right height? That’s not really surprising since ‘txtFld’ doesn’t exist anywhere there… should probably be lblFld.height no?

You’re absolutely right - all references should be to lblFld. I have them named here as txtFld and replaced them posting it here (for a bit of legibility) but obviously missed out that occurence. Apols for the confusion.

Can you show the label class ? Is it just straight extending textfield?

Here’s where I get a little embarressed about how messy my code is (still getting used to it) but:

package {	

  import flash.events.MouseEvent;
  import flash.display.*;
  import flash.events.*;
  import flash.net.*;
  import flash.utils.*;
  import flash.text.TextField;
  import fl.controls.TextArea;
  import fl.controls.Label;
  import flash.text.TextFieldAutoSize;
  import fl.events.ComponentEvent;

	public class MainStart extends Sprite{

		internal var thisNumber:Number = 1000;			
		public var rightPanel1:Sprite = new rightPanel();		
		public var rightPanel2:Sprite = new rightPanel();				
		public var rpButton1:Sprite = new rpButton();	
		public var rpButton2:Sprite = new rpButton();	


	var xmlLoader:URLLoader = new URLLoader();
			xmlLoader.addEventListener(Event.COMPLETE, showXML);
			xmlLoader.load(new URLRequest("loadClient.xml"));
			function showXML(e:Event):void {
				XML.ignoreWhitespace = true; 
				var songs:XML = new XML(e.target.data);
				trace(songs.track.length());
				var i:Number;
				for (i=0; i < songs.track.length(); i++) {
					
					addTextRP(songs.track*.band.text(), i);

				}
			}

		}
		
		public function addTextRP(thisText:String, i:Number):void {
			var thisNewNum:Number=0;
			var lblFld:Label = new Label();
			lblFld.x=0;
			lblFld.width = 160;
			lblFld.wordWrap=true;
			lblFld.autoSize = TextFieldAutoSize.CENTER;
			lblFld.y=txtHeight;
			lblFld.name = "lblFld" + i;
			lblFld.text=thisText;
			lblFld.addEventListener(ComponentEvent.RESIZE, resizeHandler());
			addChild(lblFld);

			var thisHeight:Number = lblFld.height;
			
			lblFld.addEventListenerMouseEvent.MOUSE_OVER,highlightText);
			lblFld.addEventListenerMouseEvent.MOUSE_OUT,unhighlightText);
			lblFld.addEventListener(MouseEvent.MOUSE_DOWN,dragHighlightText);	
[INDENT][/INDENT]}


		function resizeHandler(event:ComponentEvent):Number {
			var lbl:Label = event.currentTarget as Label;
			trace("width:" + lbl.width, "height:" + lbl.height);
			return(lbl.height);
		}