MrB
MrB

Reputation: 1594

AS3 a way to get TextField current size?

I was wonder if anyone knew a way to get a textfields current size?

Upvotes: 1

Views: 7191

Answers (2)

Danyal Aytekin
Danyal Aytekin

Reputation: 4215

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

Upvotes: 2

eruciform
eruciform

Reputation: 7726

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.

Upvotes: 4

Related Questions