I’ve spent the past month working on a Flash project for a large ad agency. In the agency, the designers hand me FLAs that have all of the assets, some code, and some interactivity. Rather than recreating what is on the FLA in nice, clean, object-oriented AS 2, it is my job to automate the existing assets and keep them in the FLA. In a way, the coding practice is something you might call AS 1.5. There are AS 2 classes, components, frame code, and author-time movie clips — and they all have to make friends.
They don’t make friends very well.
I’ve learned several important, poorly documented lessons from this. Here’s one of them:
Letter spacing breaks easily on author-time text fields. If there is a text field on the stage and you try to set the text via: myTextField.text = "blah blah";, it ignores the letter spacing!
The fix is easy (and a little ridiculous). Somewhere in your code, make a new temporary TextFormat instance and set it with the author-time text field’s format like this:
var fmt:TextFormat = myTextField.getTextFormat().
Then call:
myTextField.setTextFormat(fmt); .
myTextField.setNewTextFormat(fmt);
This has got to be a bug in Flash. I can’t imagine a reason Adobe would want a text field that forgets some of its text format properties and remembers others. Still, reminding a text field what its format is, seems to work just fine.
Check out my example to see a working version of this.
47 responses so far ↓
Terry // October 13, 2006 at 2:22 am
Wow a Flash Developer in Detroit that likes AS2 too… neat
Flash: Mixing Run-time elements with author time-masks « Bobspace // October 16, 2006 at 9:24 pm
[...] Bobspace Or else! « Flash: yourMom.getTextFormat(); //is the key to letterSpacing [...]
effectiveUI Blog » Blog Archive » Flash: Mixing Run-time elements with author time-masks // November 7, 2006 at 1:02 am
[...] Like I mentioned in my earlier post, I’ve been working on a large project that has a lot of AS2 mixed with densely layered FLAs, full of author-time elements. [...]
dave // February 25, 2007 at 1:45 am
Bob, thanks for the letter spacing tip – do you know of any way to have letter spacing for dynamic text fields using flash 7? We’re trying not to use the latest flash version to avoid sending people to the upgrade.
thanks
Bob Walton // February 26, 2007 at 11:40 pm
Dave, you might be out of luck. The letterspacing property of TextFormat is only available in FP8. I don’t know of any way to hack it into FP7.
However, I’d strongly consider publishing to Flash 8 instead of Flash 7. The latest stats I could find shows Flash Player 8 penetration at 94%, three points behind the older version. For me personally, that’s slim enough not to matter. And even big companies, like Yahoo for example, have been compelled by Player 8’s expanded feature set and vastly superior rendering capability and are deploying to it, despite a presumably high insistence on compatibility.
Unfortunately, a Flash project is never going to be 100% browser compatible, so by definition, you can’t help but force some visitors to upgrade. Staying in FP7 doesn’t really solve that problem for you. Since it’s way more cost effective to press ahead with FP8’s augmented feature set, than spin your wheels trying to hack it into FP7, I’d work that angle.
dave // February 26, 2007 at 11:53 pm
yeah, that’s pretty much what I was thinking too. Thanks for your response!
Bob Walton // February 26, 2007 at 11:59 pm
It’s not all bad. You get to play with Bitmaps!
Tim // March 29, 2007 at 4:10 pm
Bob, you rule. How in the hell, is this solution not posted all over the web? Do you realize what a bane this issue is to most designers? Not being able to track out dynamic text is the single biggest flaw in flash IMHO. Thanks!
Justin Stolle // March 30, 2007 at 11:47 pm
This is an extremely helpful tip for use with dynamic text fields. Thank you!
Jusitn Kipker // April 10, 2007 at 4:49 pm
Thank you thank you thank you. The letter spacing was driving me mad!
Works now though.
_justin
steven baughman // June 27, 2007 at 9:14 pm
Note: This doesn’t work unless there are characters in the text field you are trying to get the format from. Weird.
Thanks for pointing me in the right direction!
Susan Kitchens // July 2, 2007 at 7:42 pm
The link to the fla is 404.Did you move it or delete it? I’d love to look at how you did it.
I hungrily read this post and the code, thinking, “Aha! my problem is now solved” but I’m either too new and dim-witted when it comes to implementing your example code here, or there’s some other problem. Would love to poke around your file to see what it does. Is it still available?
(I’m using Flash 8 Professional)
Susan Kitchens // July 2, 2007 at 9:26 pm
er, still interested in seeing how you did it, but I fixed the problem on my end (of course, it was user error, too. Figures). Steve’s comment that characters inside the dynamic text area are also helpful: Just typing a period . inside helped make the format stick.
Bob Walton // July 2, 2007 at 9:48 pm
I tried to email the source to you. Email bob at bobspace.com and I’ll send it your way.
jche // August 18, 2007 at 12:08 am
thank you thank you thank you – this was a life saver.. some other solutions suggested on other forum were pretty scary.. thanks again!
simon // August 24, 2007 at 12:23 pm
Hi,
I’d really like to get my hands on that fla-file
simon (a) skivback . se
Dave // September 27, 2007 at 2:34 am
Nice work on the find. I had the same problem till I read that.
David // October 4, 2007 at 10:26 pm
Thanks a lot!!! I was getting mad trying to fix what was going wrong.
Sad thing is that the BUG is still on Flash 9 AS3!!!
For those seeking the FLA the code for As3 is as simple as:
var fmt:TextFormat = my_TextFiled.getTextFormat();
my_TextFiled.text = “Hello World”;
my_TextFiled.setTextFormat(fmt);
John // October 12, 2007 at 12:20 am
Hi guys,
I couldn’t get the above solution to work. Maybe because I didn’t have an instance of textFormat to begin with? I was using htmlText. But I found this solution to work, too:
var my_fmt:TextFormat = new TextFormat();
my_fmt.letterSpacing = 1;
// set the text format
myTextField.setTextFormat(my_fmt)
Andy // December 19, 2007 at 6:00 pm
Oh, and did not know about it. Thanks for the information …
Shawn // December 19, 2007 at 6:52 pm
This is not a bug, it’s just the way it is supposed to work.
You have to retrieve the current values of the instance’s format – which you don’t have a reference to until you create it.
You can’t retrieve or set format properties directly, that’s the way it’s designed. This is strict programming, I’m sure there is a good technical reason why it has been maintained through CS3.
As far as the letterSpacing issues – just make sure you’re in Flash 8 and you’ll be good with both static and dynamic instances.
Bob Walton // December 19, 2007 at 8:09 pm
Shawn, you’re missing the point a little bit.
This issue has to do with letter spacing being set in design time and then forgetting its own settings at run time. This has absolutely nothing to do with setting format properties directly.
I haven’t checked CS3 for this issue, but it definitely happens in Flash 8, which was the version I was using when I discovered this.
I’ve been able to find and repost the FLA, which should clear up the confusion.
Mikkel // December 20, 2007 at 3:08 pm
Thanks a lot! Really helpful.
FIY: It doesn’t work if you’ve set the textField’s Var.
Eric // January 13, 2008 at 7:16 pm
I had information pulling from MySQL into PHP through swfobject into the variables.
It seems a little ridiculous to have call both setTextFormat and setNewTextFormat.
Thanks for your help!
Danny // February 26, 2008 at 6:45 pm
Thanks for the post. However, like another reader, it doesn’t seem to be working for me and I can’t understand why. The only thing I deviated with was my variable name (using my own).
Any chance I can see the source code too?
matt // March 8, 2008 at 1:34 am
This bug had been driving my crazy. Thanks for this solution – it works a treat!
Pat // April 2, 2008 at 9:34 pm
thanks for this, a lifesaver indeed. cheers
Valerie // April 7, 2008 at 3:50 pm
You have made a huge group of Dallas and Denver designers very happy with this long-awaited and non-documented tip. I used to keep a huge notebook back in the day with Flash 5 tips, largely undocumented by Macromedia & Adobe. Now I’ve got your site bookmarked and will send it to my whole team. Thanks so much!
Bobspace: Bob Walton » Blog Archive » Your mom is ready for a terrorist attack… // April 8, 2008 at 6:13 am
[...] you know they love their Your Mom jokes. Inside the office, your-moms fly fast and furious, and it rubs off on you [...]
Glorb // May 5, 2008 at 4:47 pm
Ahah, Flash is always funny with its little bugs
Thanks for the trick (and the as3 update) !
Marc // May 23, 2008 at 7:24 pm
Works great, the comment made about there has to be something in the txt field so that it can gettextformat, is logical but a good comment because it had me fooled…
All I did was made a box with a character in it, and in flash made it _visible = false; and had the info of the hidden field be given to the ddynamic text field i wanted letter spacing on !
thx man!
max // June 23, 2008 at 3:57 pm
this saved my day! thanks!
Jatinder Thind // July 7, 2008 at 6:34 am
Thank you very much. This tip was a real life saver for me.
ljones // August 15, 2008 at 5:01 pm
I ran into this problem. If the item is not on the stage when you call this item, or the item was called too fast on init prior to the format being applied. I had to use an added to stage event to allow for the proper time to apply the format to the textfield within a movieclip.
this.addEventListener(Event.ADDED_TO_STAGE, onAdd, false, 0, true);
public function onAdd(evt : Event) : void
{
this.removeEventListener(Event.ADDED_TO_STAGE, onAdd);
var format:TextFormat = new TextFormat();
format.letterSpacing = 3;
tuneIn_txt.setTextFormat(format);
}
Morgan494 // September 2, 2008 at 6:05 am
One addition: if the author time contents begin with a carriage return, this fix fails. Took me a while to figure that out! I changed the first character (it’s set dynamically when viewed anyway) and all was well.
dynamic letterspacing in flash // October 6, 2008 at 11:41 pm
[...] http://bobspace.wordpress.com/2006/09/28/flash-yourmomgettextformat-is-the-key-to-letterspacing/ [...]
Webgriffe | multimedia // October 7, 2008 at 8:40 am
Hope this may help italian readers. I wrote about the same solution on February 7th: http://www.cristiangalletti.com/dynamic-textfield-kerning/
Nice work. Bye.
Text in Flash | Welcome Flash World // October 8, 2008 at 4:01 pm
[...] Read more [...]
Text in Flash | Lemlinh.com // October 10, 2008 at 8:17 am
[...] Read more [...]
neil // December 8, 2008 at 4:38 am
Stumbled upon your solution, and it’s brilliant. Don’t need to use CSS or add a bunch of extra code. Nice work.
Benjamin // December 11, 2008 at 10:26 pm
I’ve had some trouble implementing this, even when i copy/pasted the code directly to my file.
For some reason this code needs to be on a different layer than the text fields otherwise it doesn’t work.
Beats me why, perhaps someone with more expertise knows.
Thanks for posting this by the way! Solves an annoying problem.
Best, Benjamin.
Benjamin // December 11, 2008 at 10:30 pm
Crap, ignore that post, still doesn’t work for me
(
Benjamin // December 11, 2008 at 10:41 pm
Oh man, me=noob.
Had the AS version in my flash publish settings set to As1.0
Works like a charm now. thanks again!
iGo // January 20, 2009 at 4:35 pm
Man…. you just solved my ages-old mystery and lot of headache, trying different home-made ridiculous work-arounds to get spacing in dynamic textfield.
Thank You Very Much!!
mike // March 5, 2009 at 1:06 pm
thank you very much
ariel sommeria // September 2, 2009 at 3:14 pm
Hi,
I’m doing some maintenance on an AS2 project and I think you just saved me a few hours digging.
thanks!
Ariel
Diego Mattei // October 19, 2009 at 4:32 pm
Very muchas gracias por el tip