Making Alpha Work on AS3 Dynamic Text Fields
Getting the alpha property to work with coded TextFields is another AS3 technique that you would expect to be a little easier, or at least more intuitive, than it is. Initially, you may expect something like this to render the text invisible:
var myTextField:TextField = new TextField();
myTextField.text = "Doesn't Work";
addChild(myTextField);
myTextField.alpha = .5;
But regardless of the alpha property, the code above will be visible. The reason is that I haven’t specified a font. This means that it will be rendered by the operating system and Flash Player will have no control over it.
With this new knowledge you may figure that setting the font will solve the problem, so you write something like this:
var myTextField:TextField = new TextField();
var myTextFormat:TextFormat = new TextFormat();
myTextFormat.font = "Arial";
myTextField.text = "Still doesn't work";
myTextField.setTextFormat(myTextFormat);
addChild(myTextField);
myTextField.alpha = .5;
But it still doesn’t work. Why? Well, I really have no idea!
Luckily, I ran across a comment left on Adobe Livedocs by Haiducii7 who suggested that by changing the blendMode to something other than NORMAL, alpha will work. The code now, looks something like this:
var myTextField:TextField = new TextField();
var myTextFormat:TextFormat = new TextFormat();
myTextFormat.font = "Arial";
myTextField.blendMode = BlendMode.LAYER;
myTextField.text = "It works!";
myTextField.setTextFormat(myTextFormat);
addChild(myTextField);
myTextField.alpha = .5;
And it worked! According to Flash Help, BlendMode.LAYER “forces the creation of a transparency group for the display object.” I guess that makes sense, but there’s nothing in the documentation that outlines all of the steps needed to make this happen.
Not even Colin Moock’s Essential ActionScript 3.0, a book I’ve treasured in multiple versions, mentions the necessity of BlendMode.
Definitely a tip worth earmarking.
-rG
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.



That’s because the text font is not embedded. Since it’s not embedded, Flash cannot apply transparency, masking, or rotation to the textfield. Using a different blend mode forces the text to be rendered to memory and *then* have the alpha applied, as if it was a bitmap, so it works.
Hi Tim,
Thanks for the info. That kind of information would be helpful in the TextField section of the documentation.
Glenn
Thankfully, once you start targeting Flash Player 10, you won’t need any sort of blendMode hack to change alpha on device text. This has been a long-standing limitation of Flash Player, and I’m extremely happy that they’ve finally addressed it.
This definitely helped me out! Such a simple solution!
You can also apply a filter, such as a blur filter… with blurX: 0 and blurY: 0.
This also forces the textfield to be rendered as a bitmap, great for internationalizing flash sites without having to embed all font characters.
There is a lot missing in Moock’s book — I invite you to look for a single chapter on sound in his sacred AS 3.0 book…
His book is great for advanced concepts in OOP, but I prefer the for everything else…
~K
In my case, Kaleb’s suggestion to apply an invisible filter works better. If layers overlap and you change orders, it may lead to strange layer cropping.
great tip, thanks!
thank you! you’re all life savers!!
How long did this take you to figure out! Thanks for saving those hours of my life.
Setting cacheAsBitmap=true also works and, I feel, makes more sense.
Thanks for posting this – I was wondering why the alpha wasn’t working and this fixed it immediately!
this post was helpfull thanks !
Thanks very much for finding this out. It just goes to show all you need to do is simply google for several hours and you will eventually find out how to do basic things!
If you are using it within a package you still need to import flash.display.BlendMode
Holy crap… I’ve been trying to figure this out for the longest time!
Thank you sooooo much for this post! I feel like such a loser for not figuring this out.
hello guys , nice example, it helps me a lot,, thank’s
Thank you very very much!
oh man.
Thanks for that simple and clear fix.
I am not down for adding weight to SWFs for fonts since I am mostly using standard fonts.
Thank you.
mytextField.alpha=0
This alone would ve been enough hahahaha! Good luck!
Thank you so much !
Thats a great, quick way to sort it !
Sadly this does not work for me. I’ve tried it several times with no luck at all.
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00002192.html
“…Transparency values are not supported for text fields that use device fonts. You must use embedded fonts to use the _alpha transparency property with a text field. “
Great! Thanks very much! Need it!
Nice work thanks – spent an hour banging my head against a wall with this one till I found your blog
)
Very helpful, thanks
It worked for me. Save me hours. Thanks
Thanks man, that really helped a lot and saved me quite a lot of time
Hi Dude ,
nice info i used it in my date chooser component
link to my component
http://www.infogroupindia.com/blog/?p=454
Tried this, but there’s a conflict that seems to occur when I try to set the letterSpacing of my TextFormat. In conjunction with the BlendMode, it causes the text to not render! Anyone found this, or have a solution?
Nice spot, that has been bugging me for years.
Thank you very much! your post was very helpful.
One more thing, I guess we should write :
myTextField.blendMode = “layer” instead of
myTextField.blendMode = BlendMode.LAYER;
Thanks again
You are a life saver!