Resize a window depending on how much text in a text field

Anybody have a clever to resize a window and text field depending on how much text is contained in it? I’ve tried counting the characters, but since every letter is not the same size, this doesn’t always work very well. Any better way to solve this? Thanks.