PDA

View Full Version : BitmapData.draw does not show text


daljit.kumar
11-08-2009, 06:37 PM
Hey Guys,

I have this very simple code snippet which does not work fine. It simply shows a white rectangle without any text in it. I have searched for this on net and this seems to be the correct way of doing it. Any idea why it is not working?

numberText = new Text();
numberText.width = 100;
numberText.height = 100;
numberText.setStyle("horizontalCenter", true);
numberText.setStyle("fontSize", 18);
numberText.text = "Hello";

var bmd:BitmapData = new BitmapData(numberText.width, numberText.height);
var uiComp:UIComponent = new UIComponent();
bmd.draw(numberText);
uiComp.x = 500;
uiComp.y = 200;
uiComp.width = 100;
uiComp.height = 100;
uiComp.addChild(new Bitmap(bmd));
this.addChild(uiComp);

mattb
11-09-2009, 05:39 PM
mx.controls.Text is a UIComponent. UIComponent based classes use the component lifecycle that doesn't actually draw the component until a short time after you've set its properties. The reason your code isn't showing anything is that you haven't allowed the Text component to draw itself before you redraw it onto another component.

I haven't tested this, but you should probably add an event listener to numberText so you know when the component has drawn itself before doing anything with it. Try the updateComplete event. eg:


numberText = new Text();
numberText.width = 100;
numberText.height = 100;
numberText.setStyle("horizontalCenter", true);
numberText.setStyle("fontSize", 18);
numberText.text = "Hello";
numberText.addEventListener(FlexEvent.UPDATE_COMPL ETE, onTextUpdated);

function onTextUpdated(event:FlexEvent):void
{
var bmd:BitmapData = new BitmapData(numberText.width, numberText.height);
var uiComp:UIComponent = new UIComponent();
bmd.draw(numberText);
uiComp.x = 500;
uiComp.y = 200;
uiComp.width = 100;
uiComp.height = 100;
uiComp.addChild(new Bitmap(bmd));
this.addChild(uiComp);
}

Dr.Mabuse
11-09-2009, 06:42 PM
You can just force the update of the Text component with a call to the validateNow() method. Not as elegant as the solution mattb proposed, but you don't have to deal with event listeners and such for this to work.

Ref: http://livedocs.adobe.com/flex/3/langref/mx/core/UIComponent.html#validateNow%28%29