1
votes

I'm an Actionscript beginner. Eventually I want to write some code that arranges objects on the stage, and to do that I need to know their dimensions. I'm getting behavior I didn't expect from the 'width' property. In the following, no matter what text I use in my TextField (whether it is narrow or wide) the 'width' property is 100. How do I find out what the actual width of the drawn pixels is? Or the actual height? This would be like finding a bounding rectangle, as Qt would call it. How about if I want to include all the children within the bounding rectangle?

public class App1 extends Sprite {

public function App1() {
  var t:TextField = new TextField();
  t.text = "Foo Foo";
  addChild(t);
  trace(t.width);
}
}
1

1 Answers

1
votes

That's because, by default, the text in a TextField do not impact the actual textfield size. Imagine, you put a textfield to collect a user email, you do not want the textfield to strech if the user ads more and more text ... So by default, a new Textfield is 100 px wide, no matter what text it contains. You can, however make a TextField dynamically grow by messing with the autoSize property.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html#autoSize

try this :

          var t:TextField = new TextField();
          t.autoSize = TextFieldAutoSize.LEFT;
          t.text = "Foo Foo qdfqsdfqsdfq sdfqsdfqsdfqsdf qsdf";
          addChild(t);
          trace(t.width);

Keep in mind that, if multiline is set to true, the textfield will not widen, it will get to the next line ...