DrawString() to fit text to a Rectangle in XNA

It seems that there are a number of implementations of fitting several lines of text (of a specific size) into a rectangle and wrapping them (we wrote one too), but I was surprised that SpriteBatch.DrawString() did not have a form which does the more simple task of just taking a string and making it grow/shrink to fill a rectangle.

Desired Effects

White wins!What I wanted to accomplish, was to take a (probably short) arbitrary string and make it as big as possible while fitting inside of a certain box on the screen. Specifically, I wanted to show who won in the Game Over screen for Hive.

So we specified a rectangle (shown in red around the text) and made the text change its size to fit in the box without wrapping, regardless of what that text might be.

The Code

I put this static method into a utility class I used a bunch:

        /// 
        /// Draws the given string as large as possible inside the boundaries Rectangle without going
        /// outside of it.  This is accomplished by scaling the string (since the SpriteFont has a specific
        /// size).
        /// 
        /// If the string is not a perfect match inside of the boundaries (which it would rarely be), then
        /// the string will be absolutely-centered inside of the boundaries.
        /// 
        /// 
        /// 
        /// 
        static public void DrawString(RelativeSpriteBatch spriteBatch, SpriteFont font, string strToDraw, Rectangle boundaries)
        {
            Vector2 size = font.MeasureString(strToDraw);

            float xScale = (boundaries.Width / size.X);
            float yScale = (boundaries.Height / size.Y);

            // Taking the smaller scaling value will result in the text always fitting in the boundaires.
            float scale = Math.Min(xScale, yScale);

            // Figure out the location to absolutely-center it in the boundaries rectangle.
            int strWidth = (int)Math.Round(size.X * scale);
            int strHeight = (int)Math.Round(size.Y * scale);
            Vector2 position = new Vector2();
            position.X = (((boundaries.Width - strWidth) / 2) + boundaries.X);
            position.Y = (((boundaries.Height - strHeight) / 2) + boundaries.Y);

            // A bunch of settings where we just want to use reasonable defaults.
            float rotation = 0.0f;
            Vector2 spriteOrigin = new Vector2(0, 0);
            float spriteLayer = 0.0f; // all the way in the front
            SpriteEffects spriteEffects = new SpriteEffects();

            // Draw the string to the sprite batch!
            spriteBatch.DrawString(font, strToDraw, position, Color.White, rotation, spriteOrigin, scale, spriteEffects, spriteLayer);
        } // end DrawString()

Usage looks something like this:

        RelativeSpriteBatch batch;
        SpriteFont font;
        // ...
        Rectangle outcomeBoundaries = new Rectangle(100,100,500,500); // 500x500 rectangle with top left at (100, 100)
        string outcomeString = "White always wins!"; // 1984 reference 😉
        MotiveUtil.DrawString(batch, font, outcomeString, outcomeBoundaries);

The Results

Here’s how it handles some other inputs to try to fill the rectangle as much as possible without overflowing. The red box showing the boundaries is just for demonstration, it was removed from the last picture so you could see what this is actually meant to look like.