[SOLVED] Text width?

In VB and I think c# there is the property TextWidth which you can use to return the pixel width of text so you can decide how to display what you want to say.

Is there anything in Monogame that returns the physical size of text before it’s rendered? Reason, so you know where to put line brakes in paragraphs?

Thanks.

You can use SpriteFont.MeasureString. Note that it’s a bit of an expensive operation, so if you need to reuse the returned value you should cache it.

1 Like
                        spriteBatch.DrawString(Game1.font1, "Click Play Area To Initiate Particle Reaction", new Vector2(Game1.renderWidth / 2 - Game1.font1.MeasureString("Click Play Area To Initiate Particle Reaction").X / 2, (int)Game1.centre.Y - 200), Color.White);

Exactly an example of not caching the value, as this string wont change often :wink:

Never occurred to me prior to this thread to cache the value, will do in future though :slight_smile:

That’s brilliant, thank you. And good advice too. :yum:

1 Like

Unfortunately caching wont work for dynamically changing text such as lines of text with a Fps count or some other arbitrarily changing text in it.