Friday, August 26th, 2005 |
|
Textfield, newline, and CRLF |
Flash is not retarded and SWF Studio is not broken. What the hell am I talking about?
I’ve been asked a few times about carriage-returns, linefeeds, and new lines with regards to SWF Studio and Flash textfields.
Since you can read and write text files from and to the user’s system with SWF Studio and Flash, inevitably someone will want to place some of this text in a Flash textfield. Well there is a behaviour of Flash textfields that is not documented: text written into a Flash textfield will have \n (linefeed) characters converted into \r (carriage-return) characters. This means that when you dump text into a textfield and then process that text later, your text is different. Any processing routines will have to account for this conversion.
So how does this relate to SWF Studio? Text files saved on PCs contain new line characters that are composed of both a carriage-return and linefeed character - \r\n. When you read a text file and write the text to a textfield the \r\n is converted into \r\r, which will appear as two new lines. This is rather annoying but easy to work around.
Example:
function textToFlashTF(text)
{
return text.split('\r\n').join('\r');
}
Usage:
myTextfield.text = textToFlashTF(textData);
Where textData is the data received from a readFile command in SWF Studio.
So now the text looks fine in the textfield. But when we write the text to a text file the resulting file will not be a properly formatted PC file since the new lines are \r characters. Try it and open the text file in Notepad and you’ll see square “blocks” where new lines are supposed to be. Those are non-printable characters.
So the solution is as easy as the first one:
function textToFile(text)
{
return text.split('\r').join('\r\n');
}
Usage:
textData = textToFile(myTextfield.text);
Where textData is data that will be fed to a writeToFile or appendToFile command.
The Flash documentation does not state anything about this for textfields, which is an oversight. But the documentation about the newline keyword is simply incorrect. The documentation states that newline is equivalent to \n. Yet if you trace it out, you’ll find that it is not:
trace(newline == '\n'); // false
Try this instead:
trace(newline == '\r'); // true
I’ve made comments in the LiveDocs (Textfield.text and newline) regarding these oversights but it is likely nothing will be done until Flash 8 ships, since all the MM people are undoubtedly working hard on that.










A google search for “flash crlf” lead me here. This is exactly what I needed. Thanks for the quick and easy solution.
Thanks.
Big Thanks
Thank you sooooo much, I was about to kill myself…
there seems to be lack of official documentation on this.
also, i searched for the livedocs for as2, and i couldn’t find it. the links in your post don’t work. those livedocs where great because of the comments right on the documentation pages. anywhere i can find the livedocs with comments or has this docs/comments paged been completely removed?