1
votes

I'm building text balloons to display variable length messages in Flash. My question is pretty simple, though the answer may not be.

I have:

  • The string I want to display.
  • The font information.
  • The width/height ratio I want the text field to have.

How do I calculate the width and height of the text field it needs to display the text I give it, nothing more, nothing less?

2
do you use a monospaced font? ;)a--m
Maybe I'm not understanding your question, but if you create a textfield object, than than set your text, can't you simply read the width of the textObject? like: var tf:TextField = new TextField(); tf.autoSize = TextFieldAutoSize.LEFT; tf.text = 'string to display'; trace (tf.width, tf.height)a--m

2 Answers

6
votes

Easiest thing I can think off is creating a textfield, setting the custom font and text then getting the size:

e.g.

var field:TextField = new TextField();
field.defaultTextFormat = new TextFormat('Verdana',12,0xDEDEDE);
field.text = 'someText';
trace(field.textWidth + ' / ' + field.textHeight);

textWidth and textHeight should give you the right numbers, as opposed the width and height properties. If you need more details than this, have a look at the TextLineMetrics class.

HTH, George

1
votes

if you turn on autoSize it should adjust the height of your text field for you. You can then just get the height using the same field.textHeight

var field:TextField = new TextField();
field.autoSize = "left";
field.multiline = true;
field.defaultTextFormat = new TextFormat('Verdana',12,0xDEDEDE);
field.text = 'someText';
trace(field.textWidth + ' / ' + field.textHeight);