[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Creating multiple dynamic textfields through AS

I currently ahve a setup where I create one textfield through AS. This is working for me - however what im looking to do is create multiple textfield based on data from an array.

I've been looking on google hours and haven't found any workable solution.

Do anyone have an idea on how to create multiple textfields based on the array at top in the snippet and the below code?
//Create textfields based on data in Array - in this case 3 textfields
var textArray:Array = new Array('First TextField','TextField Two','Anything, really');

// Creating font instance
var garageInstance:Font = new garage();

//Creating the textfield object and naming it "myTextField"
var myTextField:TextField = new TextField();

//Here we add the new textfield instance to the stage with addchild()
containerText.addChild(myTextField);

myTextField.width = 930;
myTextField.embedFonts = true;
myTextField.multiline = true;
myTextField.wordWrap = true;
myTextField.selectable = false;

var myText:String = "testing test";
myTextField.htmlText = myText;

//This last property for our textfield is to make it autosize with the text, aligning to the left.
myTextField.autoSize = TextFieldAutoSize.CENTER;

//This is the section for our text styling, first we create a TextFormat instance naming it myFormat
var myFormat:TextFormat = new TextFormat();

//Embedding font
myFormat.font = garageInstance.fontName;
myFormat.color = 0xFFFFFF;
myFormat.size = 46;
myFormat.align = TextFormatAlign.CENTER;

myTextField.setTextFormat(myFormat);

Open in new window

0
mrsam
Asked:
mrsam
  • 2
  • 2
1 Solution
 
Jeremy DaleySenior DeveloperCommented:
It's pretty simple actually. You just run through a "for" loop of your array.
//Create textfields based on data in Array - in this case 3 textfields
var textArray:Array = new Array('First TextField','TextField Two','Anything, really');

// Creating font instance
var garageInstance:Font = new garage();

//This is the section for our text styling, first we create a TextFormat instance naming it myFormat
var myFormat:TextFormat = new TextFormat();

//Embedding font
myFormat.font = garageInstance.fontName;
myFormat.color = 0xFFFFFF;
myFormat.size = 46;
myFormat.align = TextFormatAlign.CENTER;

for each (var myText:String in textArray)
{
    //Creating the textfield object and naming it "myTextField"
    var myTextField:TextField = new TextField();
    myTextField.defaultTextFormat = myFormat;
    myTextField.width = 930;
    myTextField.embedFonts = true;
    myTextField.multiline = true;
    myTextField.wordWrap = true;
    myTextField.selectable = false;
    myTextField.htmlText = myText;
    //This last property for our textfield is to make it autosize with the text, aligning to the left.
    myTextField.autoSize = TextFieldAutoSize.CENTER;

    //Here we add the new textfield instance to the stage with addchild()
    containerText.addChild(myTextField);
}

Open in new window

0
 
mrsamAuthor Commented:
Lol you posted this the second I logged on here, nice timing :)

"var myText:String in textArray" that's what i've been searching for all day I guess! So wicked daleyjem.

Would I be able to reference to each individually textfield somehow? Since I need to position the different textfields different places.
0
 
Jeremy DaleySenior DeveloperCommented:
well, you could position them within the "for" loop.

or to reference them later, you'd have to create the instance of each textfield outside of the "for" loop, giving them each unique variable names. otherwise, you could push each TextField object into a different array, and later reference each object by the index of that array. make sense?

are you adding them vertically down a list? in that case, you could just create a:
var currY:Number = 0;

...and then at the end of each "for" iteration, you could do:
currY += myTextField.height;
0
 
mrsamAuthor Commented:
Thanks for the answer daleyjem - in this case im not looking to place them under eachother vertically - but nice to know hows that done.

Im not sure how to create the instance of each textfield outside of the for loop, but I think the part about pushing it into another array made somewhat sence.

I've tried the following:
// Added at top
var referenceArray:Array = new Array();

//Added under the addChild
referenceArray.push(myTextField);

//Then I tried to trace out the array
trace("1 textfield in array "+referenceArray[0]);
trace("2 textfield in array "+referenceArray[1]);
trace("3 textfield in array "+referenceArray[2]);

//Which traced out the following, which im not sure how to use as a reference
1 textfield in array [object TextField]
2 textfield in array [object TextField]
3 textfield in array [object TextField]

Open in new window

0
 
CWS (haripriya)Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now