Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
wmatt
Posts: 125
Registered: ‎10-02-2011
My Device: Playbook

BB10: Update Label textWidth

The problem is I can't update the width of a label after it's been set. This sample code sets a label and updates the text after 5 seconds, but the width doesn't change.

 

package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import qnx.fuse.ui.text.Label;
	
	public class LabelWidthTest extends Sprite
	{
		public var label:Label;
		
		public function LabelWidthTest()
		{
			super();
			
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			init();
		}
		
		private function init():void {
			label = new Label();
			label.text = "0123456789abcdefghijklmnop";
			label.width = stage.stageWidth;
			label.height = stage.stageHeight;
			label.x = 10;
			label.y = 10;
			addChild(label);
			label.validateNow();
			label.width = label.textWidth;
			label.height = label.textHeight;
			trace("Initital text width = "+label.width);
			var timer:Timer = new Timer(5000, 1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, OnTimerComplete);
			timer.start();
		}
		
		protected function OnTimerComplete(event:TimerEvent):void
		{
			label.text = "0123456789";
			label.width = stage.stageWidth;
			label.height = stage.stageHeight;
			label.validateNow();
			label.width = label.textWidth;
			label.height = label.textHeight;
			trace("Changed text width = "+label.width);
		}
	}
}

 

Please use plain text.
Developer
pyth
Posts: 506
Registered: ‎01-19-2011
My Device: My Trusty Red Plane

Re: BB10: Update Label textWidth

can you try to set the textformat, before you set the text? I think I had that problem before, too, and when I look at my code now, the first thing I do after calling the construktor is to set the format :/?

-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
Please use plain text.
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: BB10: Update Label textWidth

If you did a setTimeout after setting the text, does the callback function allow you to set the size properly?
Please use plain text.
Developer
wmatt
Posts: 125
Registered: ‎10-02-2011
My Device: Playbook

Re: BB10: Update Label textWidth

Adding a TextFormat had no effect. I tried repeatedly changing the text and width. Seems like the textWidth property doesn't update when text changes.

 

package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import qnx.fuse.ui.text.Label;
	import qnx.fuse.ui.text.TextFormat;
	
	public class LabelWidthTest extends Sprite
	{
		public var label:Label;
		
		public function LabelWidthTest()
		{
			super();
			
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			init();
		}
		
		private function init():void {
			var textFormat:TextFormat = new TextFormat();
			textFormat.size = 32;
			textFormat.bold = true;
			textFormat.color = 0x404040;
			label = new Label();
			label.format = textFormat;
			label.text = "0123456789abcdefghijklmnop";
			label.width = stage.stageWidth;
			label.height = stage.stageHeight;
			label.x = 10;
			label.y = 10;
			addChild(label);
			label.validateNow();
			label.width = label.textWidth;
			label.height = label.textHeight;
			trace("Initital text width = "+label.width);
			var timer:Timer = new Timer(3000);
			timer.addEventListener(TimerEvent.TIMER, OnTimerComplete);
			timer.start();
		}
		
		protected function OnTimerComplete(event:TimerEvent):void
		{
			label.text = "0123456789";
			label.width = stage.stageWidth;
			label.height = stage.stageHeight;
			label.validateNow();
			label.width = label.textWidth;
			label.height = label.textHeight;
			trace("Changed text width = "+label.width);
		}
	}
}

 

Please use plain text.
Developer
pyth
Posts: 506
Registered: ‎01-19-2011
My Device: My Trusty Red Plane

Re: BB10: Update Label textWidth

ok, two more possibilities.

1) I'm still on the old SDK, so I don't know what 'validateNow' does, but can you try commenting it out at both places?

 

2) It could be a bug, in which case you should report it. Because if you use plain old AS3 (see below), it works, though you have to use a TextField, set the defaultFormat instead of the format, and add a bit of a buffer depending on your fonts (more if you use many serifs). As an example, I used 15

 

The code below should give you this:

 

Initital text width = 426
Changed text width = 175

 

I just did both possibilities, but you can play around a bit more

 

package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
//	import qnx.fuse.ui.text.Label;
//	import qnx.fuse.ui.text.TextFormat;
	
	public class LabelWidthTest extends Sprite
	{
		public var label:TextField;
		
		public function LabelWidthTest()
		{
			super();
			
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			init();
		}
		
		private function init():void {
			var textFormat:TextFormat = new TextFormat();
			textFormat.size = 32;
			textFormat.bold = true;
			textFormat.color = 0x404040;
			label = new TextField();
			label.defaultTextFormat = textFormat;
			label.text = "0123456789abcdefghijklmnop";
			label.width = stage.stageWidth;
			label.height = stage.stageHeight;
			label.x = 10;
			label.y = 10;
			addChild(label);
//			label.validateNow();
			label.width = label.textWidth + 15;
			label.height = label.textHeight + 15;
			trace("Initital text width = "+label.width);
			var timer:Timer = new Timer(3000);
			timer.addEventListener(TimerEvent.TIMER, OnTimerComplete);
			timer.start();
		}
		
		protected function OnTimerComplete(event:TimerEvent):void
		{
			label.text = "0123456789";
			label.width = stage.stageWidth;
			label.height = stage.stageHeight;
//			label.validateNow();
			label.width = label.textWidth + 15;
			label.height = label.textHeight + 15;
			trace("Changed text width = "+label.width);
		}
	}
}

 

-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
Please use plain text.
Developer
pyth
Posts: 506
Registered: ‎01-19-2011
My Device: My Trusty Red Plane

Re: BB10: Update Label textWidth

[ Edited ]

hohoho, and if that shouldn't work, try to modify your label's textfield :smileyhappy:

 

I just looked into the API, and the qnx.fuses.ui.label has a property named 'textfield', which IS a plain old flash AS3 textfield :smileyvery-happy:. Try to set the default textformat there and read the textwidth/height directly from that component. Maybe that works :smileywink:

-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
Please use plain text.
Developer
Developer
mdd
Posts: 224
Registered: ‎01-17-2012
My Device: PB

Re: BB10: Update Label textWidth

I'm having a similar issue on DevAlpha simulator:

 

The following code is in the updateDisplayList() of a CellRenderer:

	label.x = _offset + h + 5;
	label.width = w - label.x - _dw;

	label.validateNow();

	if ( label.textWidth > label.scrollRect.width )
	{
		var lm:LayoutMeasurement = label.measure( DefaultSize, DefaultSize );
						
		trace( label.width, w, label.x, label.text,
label.textWidth, "x", label.textHeight, lm.preferredWidth, "x", lm.preferredHeight,
label.layoutComplete ); }

 typical output:

235 420 175 Recent 236 x 48 111 x 48 true
235 420 175 My Music 297 x 48 153 x 48 true

The measured width is completely inaccurate, and moreover, some entries even overwrite their scrollRect.

 

When that happens, the label can be dragged horizontally, but cannot be selected.

Please use plain text.