Alpinista
Alpinista

Reputation: 3741

textField textWidth is incorrect ActionScript 3

I have a dynamic textField using a postscript font (using the Classic font engine in CS5). I created the textField at author time. When I try to determine the actual width of the textField using the textField.textWidth property, the returned width is wildly wrong. For example, the text as displayed on screen has an approximate width of 350 pixels. but the returned width is only 150 pixels. I have tried switching fonts, and using the TextLineMetrix to no avail.

Any ideas why I am not getting an accurate width?

Upvotes: 3

Views: 7954

Answers (3)

Roy
Roy

Reputation: 325

I find that getBounds give the closest results:

var bounds:Rectangle = textfield.getBounds(textfield.parent);

Upvotes: 2

StapleGun
StapleGun

Reputation: 740

Make sure you set the autoSize property of the text field, this will cause flash to recognize the width as the width of the text instead of the width of the containing text field. Try something like this...

myTextField.autoSize = TextFieldAutoSize.LEFT;
trace(myTextField.textWidth);

Upvotes: 9

user616783
user616783

Reputation: 1

var str:String = "hello world";
mytf.autoSize = true;
mytf.text = str;
trace(mytf.textWidth )
trace (mytf._width)

62 and 63.8

Weird:S

Upvotes: 0

Related Questions