TextFieldAutoSize and textWidth in AS3

I am trying to draw a background in a text box in AS3.

I have a TextField with an embedded font and using autoSize = TextFieldAutoSize.LEFT. I assign the text to TextField and then call try a draw roundedRect using textWidth TextField. The width of the text field is always less than the actual width of the text.

Is there any other way to get the actual width of the text? I did some quick google searches, but I didn't find anything.

the code:

var tfProgramName:TextField = TextUtil.createTextField(true,"Arial",20,true);
tfProgramName.width = 100;
tfProgramName.autoSize = TextFieldAutoSize.LEFT;
tfProgramName.x = 5;
tfProgramName.y = 5;
addChild(tfProgramName);

tfProgramName.text = _program.title;
background.graphics.clear();
background.graphics.beginFill(0xFF0000,0.75);
background.graphics.drawRoundRect(0,0,tfProgramName.textWidth+10,this.height+10,5,5);
background.graphics.endFill();

textWidth in my case is something like 373, but it should be closer to 400. It is definitely close, but it doesn't seem to take into account the font size or font.

+5
source share
3 answers

, , TextLineMetrics , - , , , .

+1

, TextField getCharBoundaries() getCharIndexAtPoint(). TextField TextWidth - . LineMetrics , , .

0

TextField .width, .textWidth, TextWidth. TextField, x, TextLineMetrics, , :

var lineMetrics : TextLineMetrics = textField.getLineMetrics(0);
var startX : Number = lineMetrics.x;

and the width will be either:

lineMetrics.width;

or

textField.textWidth;
0
source

All Articles