Subscribe to RSS
get email updates
home | about | pixDif AIR app | video tutorials
polyGeek.com

Flash 9 TextFields and the added a carriage return character

June 15th, 2007 . by polyGeek

It seems that the Textfields in Flash 9/CS3 give you a little something extra: the carriage return (CR) character. It is automatically added to the end of the text value of each Textfield. Even if the Textfield is empty it will still have a CR.

This shouldn’t be a problem for most cases and only applies if you create dynamic Textfields at authoring time. Textfields created at runtime are truly empty when you create them.

The main case where you might have an issue with this is if you need to test a Textfield created at author time to see if it’s empty. If you write the following code you will get the else result.

Now if you want to test and see if the Textfield is empty you will have to do this:

Another problem you might have is if you want to append text ( someTF.text += someString ) to a Textfield created at authoring time from various locations in your code. If you don’t know when you will append first your first line of text will appear one line lower than you expected. You could solve the problem with:

You could also solve this problem with an init() function whereby you make all your authoring time Textfields equal to an empty string. If there are only a few Textfields it’s nothing to just target them individually. Otherwise you might want to do a for(in loop) like such:

There is only one problem: this will wipe out the text of any static Textfields you might have as well. You can’t give a static TextField an instance name at authoring time but they do get instance names at runtime along with all the other Stage objects: instance1, instance2, instance3, …

The only way that you could wipe out only the dynamic Textfields would be to make sure that they all have authoring time instance names and then test to make sure that the instance name doesn’t begin with the string “instance”. Here is an example:

Now you might be asking, what do I do if I have created dynamic Textfields all over the place at authoring time and I want to blank them all out in one fell swoop. Is that possible?

Well yes. Try this:

That code snippet will recursively run through every MovieClip placed on the Stage at authoring time. So this might be a kick in the pants to your startup time if you have a huge SWF with hundreds, or even thousands, of embedded MovieClips.

I’ll leave it as an exercise for the reader to make their own class out of this if they wish.

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

6 Responses to “Flash 9 TextFields and the added a carriage return character”



[...] returns New with CS3 is the added carriage return at the end of all authoring time TextFields – read read more about it.. That can cause big problems when you are reading the text value of a TextField. This little [...]

comment number 2 by: Audas

Thankyou, ……..frustrating day.

comment number 3 by: Kevin Lassard

Thank you!
I spent the entire day examining my code (which I now have to format myself since auto format in CS3 is schizophrenic). Has Adobe secretly been taken over by Microsoft or Serious Cybernetics?

comment number 4 by: polyGeek

@Kevin, nice H2G reference. Yeah, Adobe definitely wasn’t on top of their game with the TextField.

polyGeeks last blog post..Interview with Jeremy Saenz, a.k.a. The Flex Gangster

comment number 5 by: mark

Hooooooooooooooly balls this problem has been bugging me for probably like a freaking YEAR I LOVE YOU.

well my exact problem was with loading lines of text via URLLoader, but it wasn't splitting correctly because I was just splitting on "
" but now I have it doing both. yes!

comment number 6 by: polygeek

@Mark: I love it when something that I’ve written helps someone like you out. Just about everything I blog about was a pain in my ass for a while. That’s why it’s here.

   Welcome back (Change)

Leave a Reply

comment feed RSS   subscribe to this comment thread

Recent Posts

   



polyGeek.com

© Copyright 2008 polyGeek.com / Dan Florio, All Rights Reserved Except Where Explicitly Stated
Web Developement Blogs - Blog Catalog Blog Directory
M2 Websites
Local Directory for Los Angeles, CA

Better Tag Cloud