开发者

AS3 a way to get TextField current size?

I was wonder if anyone knew a way to get a textfields current si开发者_开发百科ze?


In general, the current position, width, and height are stored in:

textfield.x
textfield.y
textfield.width
textfield.height

Though it might not be set until it's positioned within something that's at some point connected to the stage. Otherwise, it hasn't figured out how big it is yet, in case it needs to squeeze into a smaller outer object or something.

You can trap when it's added to the stage with

textfield.addEventListener( mx.events.Event.ADDED_TO_STAGE, handleAddedToStage );
...
protected function handleAddedToStage( event:Event ):void
{
  ...

Update:

User wants text-size. I believe the answer is:

textfield.getTextFormat().size

Where there are optional args to getTextFormat() to pick a range of text within the field.


If you're applying multiple text formats to a TextField, then you might actually be in luck, because AS3 will start to represent the TextField using HTML.

You can get at the HTML by writing:

yourTextField.htmlText

This will give you something like:

<P ALIGN="LEFT">
    <FONT FACE="Georgia" SIZE="12" COLOR="#666666" LETTERSPACING="0" KERNING="0">
        Smaller text.
    </FONT>
    <FONT FACE="Georgia" SIZE="16" COLOR="#666666" LETTERSPACING="0" KERNING="0">
        Bigger text.
    </FONT>
</P>

You could then parse this string and search for the SIZE attributes. In the example above they'd be 12 and 16 pixels.

If you're just applying a single text format, you can go ahead and use eruciform's suggestion:

yourTextField.getTextFormat().size
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜