08.10Actionscript 3 TextField renders excessive line breaks
Hi all,
I was working on a project for musicnation.com, their EPK. I had to render text in a textfield that was coming from an XML feed, text that is entered by users. It can be filled with different types of line feeds, notable the:
\n = linux
\r = mac (prior to macos being based on freebsd I guess)
\r\n = the damned pc linefeed.
The TextField object in Flash seems to render both \r and \n as linefeeds. Therefore I was getting way too many linefeeds in my rendered text.
Here is my solution:
Actionscript:
-
var pcLB:RegExp = /\r\n/g;
-
var macLB:RegExp = /\r/g;
-
var linuxLB:RegExp = /\n/g;
-
-
_text = _text.replace(pcLB, "\n");
-
_text = _text.replace(macLB, "\n");
Tada! _text is of type String. The /g is VERY important at the end of the declaration of the RegExp, it means it will replace ALL instances. If you do not put /g it wont.
Martin

Thanks for this mate!
May 13th, 2008 at 10:31 pm
Awesome, this was driving me mental !
October 22nd, 2008 at 1:54 am
thanks for this tip, very useful
November 12th, 2008 at 8:48 am
Juhuu!
February 7th, 2009 at 2:31 pm
thank you this was a great quick solution!
June 6th, 2009 at 12:49 pm
Thanks!
It also works to get rid of line breaks!
August 20th, 2009 at 2:04 am
Very useful tip.
Thanks for sharing! =)
October 21st, 2009 at 10:21 am
thanks for the tip!
RegExp are so usefull in many langages, but very tricky to implement…
December 7th, 2009 at 8:07 am
You really saved my day! Great!!
December 15th, 2009 at 6:39 am
Thanx = )
May 25th, 2010 at 3:25 am