In Actionscript 2, why is this TextField _height inconsistent?
I've created a dynamic text field, and set autoSize to true, so it can grow was tall as necessary. I then want to use the _height value to size a container graphic (speech bubble).
Here's the (simplified) code:
var format:TextFormat = new TextFormat("Arial", 14);
format.color = 0x000000;
format.letterSpacing = -0.2;
var txt:TextField = _root.createTextField("dialog_txt", 150, 10, 10, 150, 0);
txt.multiline = true;
txt.wordWrap = true;
t开发者_StackOverflowxt.autoSize = 'left';
txt.embedFonts = true;
txt.selectable = false;
txt.text = value;
txt.setTextFormat(format);
I then try to set my container's height to the height of the textfield + 5, but it comes out way too short.
container._height = txt._height + 5;
I found that by adding trace(txt._height)
before, the values come out right, even though the trace value is too small. Even just assigning the txt._height to a temp variable (var junk=txt._height;
) fixes the problem. Replacing that with a couple trace functions:
trace(txt._height); //Note: No code between these two calls
trace(txt._height);
Returns 19.6
followed immediately by 35.2
. It's as though reading txt._height
causes it to recalculate, so it's correct the second time.
I also tried the textHeight
property, which also seems to get recalculated after _height
is accessed. This sequence, for example:
trace(txt.textHeight); // Returns 15
trace(txt.textHeight); // Returns 15
trace(txt._height); // Returns 19.6
trace(txt.textHeight); // Returns 31
trace(txt._height); // Returns 35.2
This is in AS 2, being compiled by MTASC.
Any ideas on what's going on here?
First off, you need to clarify that you are using AS2. I take it that means you are running Flash 8 or something along those lines (AS3 uses .height, AS2 uses ._height)?
What could be happening is the calculation only happens on a new frame. Did you try using something like setTimeout() to call your resize code later or dispatchEvent to do the resizing?
From my experience autosize never did run well. You are much better off creating a class that extends Textfield and write your own text function that calls the super, then expands the height of the textfield to _textHeight. This value should be correct the first time with autosize off.
Try using the height
property instead of the _height
property.
精彩评论