You could also get a good estimate by calculating the width from the character widths and (either mean or max) spacing/kerning, especially for the sprite font object, I would assume. If you use a monospaced font even easier of course.
I'm not sure if spritefont behaves like the text object in the way that it only knows the text width and height 1 frame AFTER the text was set. But I have hopes, that it works instantly. It if does, an iterative approach like Rojohound mentioned is probably the easiest way to go about it .
edit:
Saw the second post. It looks like it works instantly.
So you can do this:
- Spritefont set Scale to 1
- Spritefont set text to yourtext
- Spritefont set scale to clamp(spritefont.Width/spritefont.TextWidth,0,1)
(1 being the zoom factor you want at most)