This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center




已关闭10年。




我正在尝试为AS3中的文本字段绘制背景。

我有一个带有嵌入字体的TextField,并使用autoSize = TextFieldAutoSize.LEFT。我将文本分配给TextField,然后调用尝试使用TextField的textWidth绘制roundedRect的方法。文本字段的宽度始终小于实际的文本宽度。

还有另一种方法来获取实际的文本宽度吗?我做了几个快速的谷歌搜索,但我什么也没找到。

代码:
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大约是373,但应该接近400。它肯定很接近,但是似乎没有考虑字体或字体大小。

最佳答案

我不能保证会更好,但是看看TextLineMetrics它给您的信息比其他任何东西都多,所以这可能是您最好的选择。

关于flash - AS3中的TextFieldAutoSize和textWidth,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4963948/

10-12 23:04