3

I am adding a series of text fields through a loop based on an XML file. The width of the fields is always going to be 200 px so depending on how much text is contained in the XML node, the height of the text field will vary. I need a way to stack these fields on top of each other based on their height plus say a 10 px space between each. Below is how I am creating the text fields.

for(var i:int; i < xml.item.length(); i++)
{
    var theText:TextField = new TextField();
    addChild(theText);
    theText.wordWrap = true;
    theText.width = 200;
    theText.antiAliasType = AntiAliasType.ADVANCED;
    theText.autoSize = TextFieldAutoSize.LEFT;
    theText.selectable = false;
    theText.htmlText = xml.item[i].@theText;
};

1 Answer 1

3

You can keep track of the height by using the height of the text field.

var startHeight:int = 0;
for(var i:int; i < xml.item.length(); i++)
{
    var theText:TextField = new TextField();
    addChild(theText);

    theText.y = startHeight;

    theText.wordWrap = true;
    theText.width = 200;
    theText.antiAliasType = AntiAliasType.ADVANCED;
    theText.autoSize = TextFieldAutoSize.LEFT;
    theText.selectable = false;
    theText.htmlText = xml.item[i].@theText;

    startHeight += theText.height + 10;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I knew it was something simple. Only one small change for anyone else who comes across this, the first line should be: "var startHeight:int = 0;"
Haha yeah editing that too much C++/AS3 mixing does that sometimes :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.