views:

57

answers:

1
var txtIt:Text = new Text();
                txtIt.text = full_array[t][0];
                txtIt.width = 700;
                txtIt.buttonMode = true;
                txtIt.mouseChildren = false;
                txtIt.selectable = false;
                txtIt.y = t * 30;

                trace(txtIt.textWidth);

                myCanvas.addChild(txtIt);

Why can't i get the textWidth for the component? I can get it for textFields.

I've used the namespace code and i have this:

 import mx.core.mx_internal;
use namespace mx_internal;

            var txtIt:Text = new Text();



                txtIt.text = full_array[t][0];
                txtIt.width = 700;
                txtIt.buttonMode = true;
                txtIt.mouseChildren = false;
                txtIt.selectable = false;
                txtIt.y = t * 30;

                var txtfld:TextField = txtIt.getTextField() as TextField;
                trace(txtfld.textWidth);


                myCanvas.addChild(txtIt);

But I get TypeError: Error #1009: Cannot access a property or method of a null object reference.

+1  A: 

You need to use the mx_internal namespace so that you can access the TextField object of the Text component. In your ActionScript code you have to declare

import mx.core.mx_internal;
use namespace mx_internal;

and then something like

var myText:Text = new Text();
var txtfld:TextField = myText.getTextField() as TextField;

All the text measuring properties and methods are available through the TextField

Robusto
I'm getting a TypeError: Error #1009: Cannot access a property or method of a null object reference.
Adam
Show your new code.
Robusto
Iadded my new code to my main question. I'm new to this so I wasnt sure if I should put it in the comments.
Adam
Try importing flash.text.TextField as well. For more info, go to http://livedocs.adobe.com/flex/3/langref/flash/text/TextField.html
Robusto
Actually, now that I look at your question with morning brain, I have another suggestion. Create a new TextLineMetrics object and assign it the return value of txtIt.measureText(). Then examine the object in the debugger and see if you're getting all the properties of the TextLineMetrics, which will include width, height, etc.
Robusto
Thanks I did this:var metrics:TextLineMetrics = measureText(txtIt.text); trace(metrics.width);and it seems to be working.
Adam
@Adam: Cool. So mark this answer with a check and start accruing good will for future questions. :)
Robusto
ok thanks. one other thing. txtIt.width = 400; for one of the metrics.width I got 372. It is less than 400, however the text is wrapped. How is i wrapping if the width of the text is less than the width of the field.Something in the Text component is making text wrap before it reaches the max width
Adam
I think it is because of my css style changing the font's width
Adam
Could be. If it is wrapping, the textWidth will be the width of the longest line.
Robusto