getBounds for TextFields = getBitmapBounds()

โ€” ActionScript, Code snippets

Code snippetI found out the getBounds()-function gives not exactly the right rectangle with a TextField. It always matches the border bounds, even if the borders are invisible. So I’ve created a more accurate function using getColorBoundsRect().

Click on the words. Switch between getRect() and getTextFieldBounds() by clicking on the buttons below to see the difference.

ยป Download source

2 responses to “getBounds for TextFields = getBitmapBounds()”

  1. luuk says:

    Hi Mark, getBounds gets inherited from DisplayObject, so it can be expected it returns the border bounds, the methods wasn’t designed for text.

    Although your solutions seems to produce a fine result, it is kind of overengineered converting the textfield to a bitmapdata object. Have you tried looking into TextLineMetrics? (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextLineMetrics.html)

    That in combination with numLines, getLineText and getCharBounds suffices to get al the info you need to find the exact borders ๐Ÿ™‚

Say something interesting

Please link to code from an external resource, like gist.github.com.