Why can't I get textWidth from my Text component?
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 na开发者_StackOverflowmespace 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.
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
精彩评论