I 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.