The TextField
's .autosize
property is great for sizing dynamic text fields when you already know the text string (but mind the .multiline
and .wordwrap
properties), but won't be helpful for input text fields.
For input text, I'd suggest listening for the Event.CHANGE
event, then updating the width/height based on the number of lines, the .textWidth
, or TextLineMetrics
info (e.g. myTextField.getLineMetrics
).
Here's a quick example:
var myField:TextField = new TextField();
myField.x = 10;
myField.y = 10;
myField.width = 100;
myField.height = 20;
myField.border = true;
myField.type = TextFieldType.INPUT;
myField.addEventListener(Event.CHANGE, textChangeHandler);
addChild(myField);
function textChangeHandler(evt:Event) {
var buffer:Number = 10;
myField.width = Math.max(100, (myField.textWidth + buffer));
myField.scrollH = 0;
}
Edit: Oh, and if you want that to work with .multiline
, then just add:
myField.multiline = true;
and in the textChangeHandler
function add:
myField.height = myField.textHeight + buffer;