03 May 2009

Stange static text width

I'm currently playing with auto layout.
My goal is to make an application handling any screen size.

I so managed to make an extended MovieClip class which change its _x & _y according screen size.
Unfortunatly I got some problems with MovieClip with static texts inside.
I so made some tests and, well, I'm very surprised with the result.
Test it yourself :

- create a new FLA for FL2.x+
- add ascode Stage.align="TL";Stage.scaleMode="noScale"; on first frame
- create a new symbol-movieclip
- in this symbol, add a textfield at 0,0 with a width of 200 for exemple, Arial, 48, "bitmap text"
- add a "HELLO" within
- add as-code trace(this._width) on symbol 1st frame which will give you the size of the symbol which, I thought, SHOULD be 200
- run it -> trace gives 152, which is NEAR the size you'll find if you break HELLO in symbol (2 * Ctrl+B)
- use "anti alias"
- run it -> trace gives 183.55, value I don't understand !
- use "device font"
- run it -> trace gives 225, value I also don't understand !
- now, make the same tests with a centered text
- traces will give you the exact same value
- convert it to dynamic
- run it -> trace gives 200

It comes from the fact than when Flash compiles the FLA, it transforms the static text to shape and so the width of the movieclip isn't from 0 to right of textfield in author mode but from left side to right side of the converted shape. In fact, the same way width is measured with shape and movieclip inside another movieclip.

So, the main problem is the way it measures it when you have centered text OR antialiased text OR device font. The value is for me totally unusable.
For example, imagine you have some thing like that

What do you think Flash will measure ?

Flash will in fact measure something near

What do you think Flash will measure ?

so if I want to center the movieclip containing this text, using _x = (Stage.width - mclip.width)/2, the result won't be the one I hoped for :(
Even worst if I use device font!

2 "workarounds":

use the dot method
add a 1x1 dot at (0,0) and another one at (textfield_width-1,textfield_height-1) so they will be used in movieclip measure
BUT remember antialias text/device font gives really poor result, so always make your textfield larger than your text
BUT doesn't work with centertext !!!! I don't understand why HELLO centered with dots give 208.7 in width !

use dynamic textfields
nonsense but measure will be perfect
BUT it will use more memory : HELLO use 129K in static, 132K in dynamic
BUT you'll need to include font, else height won't be correct (!!)

1 comment:

Anonymous said...

Nice thanks, this helped me out :)