Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Creating a multi-line text document with AS3

New Here ,
Nov 18, 2013 Nov 18, 2013

Hello all,

I am currious if it is possible to create a multi-line text document from an input text field's .text

I am using:

function Save_Text (e:MouseEvent)

{

          fileRef = new FileReference();

          fileRef.save(TXTInputText.text, "saveFile.txt");

}

this wil lcreate the text file with the information put into the text field. The text field is set up as Input text with the behavior set to Multiline.

When the input text contains multiple lines it wil lsave it all on one line in the .txt

Ive tried using:

TXTInputText.text += "\n"

TXTInputText.text += "This is Line 2"

I have been unable to find anything in my searches online and would be greatful for the help. Thanks.

TOPICS
ActionScript
2.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 18, 2013 Nov 18, 2013

line breaks (like those created with the enter key) in the original text will be preserved in your text file.

however, to create a text file that looks like your input textfield (where you have multiline and autoWrap enabled), you'll need to parse the textfield's text string.  for example:

function tfF(tf:TextField):String{

    var s:String = "";

    for(var i:int=0;i<tf.text.length;i++){

        try{

            s += tf.getLineText(i)+"\n";

            //trace(s,i);

        } catch(e){

            break;

        }

    }

    return s.substr(0,s.length-3);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 18, 2013 Nov 18, 2013

kglad wrote:

line breaks (like those created with the enter key) in the original text will be preserved in your text file.

This isnt appearing to be true for me. When i type text into the input text field, hit enter, and type an additional line, save it, then open the saved .txt it shows up as:

Line1Line2

even though i typed:

Line1(enter)

Line2

----------------------

The function you proposed did work. And while i understand what it is doing, kinda, could you explain it to me? From what i am seeing, it is searching the textfield for the "\n" which would be where the line break is, however, after that i am a bit lost. I would like to be able to understand it and not just use it.

--------------------

That would solve one issue i have, however, here is another scenario of the same issues:

I would display to the user:

Jack saw some animals jump over some things.

the cat jumped over the _____________(Input Text Field)

the dog jumped over the  ____________(Input Text Field)

The user would input whatever they wanted into the input text fields (example: "rock" and "car") and the resulting saved .txt when opened would show:

Jack saw some animals jump over some things.

the cat jumped over the rock

the dog jumped over the car

Even though it isnt what im working on, imagine a flash version of the old mad gabs. I ask for a bunch of adjetives, verbs, and nouns. Then place them in and save the .txt

I would need to be able to do something like:

var myString:String = ""

function Save_Test (e:MouseEvent)

{

myString = "This is line 1";

myString += "\n";

myString += myTextInput.text

myString += "\n";

myString += "This is line 3";

fileRef = new FileReference();

fileRef.save(myString, "saveFile.txt");

}

I run the program, type "Hellow World" and save.

I want the .txt to read:

This is Line 1

Hello World

This is line 3

However with this setup, i get:

This is Line1Hellow World

This is Line 3

The function you presented allows me to maintain multiple lines from a multiline input, im not sure how to apply it when i have a single line input that i want inserted into a multiline text, as shown above.

thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 18, 2013 Nov 18, 2013
LATEST

open your textfile (if it exists), save its contencts to a variable, then parse your textfield's string using the function i suggested and then append that parsed string to your variable and save.

here's an explaination of tfF:

function tfF(tf:TextField):String{

    var s:String = "";

    for(var i:int=0;i<tf.text.length;i++){

          // try is used because it's unlikely there are tf.text.length lines of text

        try{

            // tf.getLineText(i) returns the ith line of text.  this code inserts a line break after each line of text. 

            s += tf.getLineText(i)+"\n";

        } catch(e){

         // once there are no more lines of text, looping can stop

            break;

        }

    }

     // strip last "\n" from above and strip the carriage return that all mutiline textfields append

    return s.substr(0,s.length-3);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines