Jump to content
  • Advertisement
Sign in to follow this  
SelethD

XNA Question Sizing a string to fit a width

This topic is 3661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Using XNA how do I get a string to fit a certain width on my screen. here is my little code that does NOT work font is the SpriteFont im using. string holds the text dim.X is holding the width in pixels that i dont want the text to be longer than while (font.MeasureString(text).X > (dim.X - 10) & text.Length > 0) { text.Remove(text.Length - 1); } when i run this and send a large string, say 30 characters, it never exits the loop is there a better way to do this?

Share this post


Link to post
Share on other sites
Advertisement
SelethD,

I'm not sure what you mean by "here is my little code that does NOT work."

Does it ever step into the while loop? Does it remove all of the characters?

The best person to debug this is you, as you're capable of seeing what the value of text, MeasureString(text).x, (dim.x-10) and text.Length are at the beginning of the loop, and each iteration after - something we cannot do.

With that being said, if it's never entering the loop then either MeasureString(text).X is less-than or equal to (dim.X-10) for some reason, or text.Length is equal to zero for some reason.

If it's continuing until all characters are removed then for some reason MeasureString(text).X is ALWAYS greater than (dim.X-10) until text.Length is equal to Zero. Perhaps your (dim.x-10) is smaller than the width of a single character?

Additionally, I notice you're using the Bitwise AND operator '&' rather than the logical AND operator '&&'. That may be your problem as leaving it as written would do a bitewise AND of the results of the two comparisons. This *shouldn't* be a problem as it's a bitwise comparison of two booleans, however, I can't recall the precise behavior of doing a bitwise AND on boolean values right now. Best to switch it to '&&' as it's more efficient anyways.

Cheers and Good Luck!

Share this post


Link to post
Share on other sites
Remove returns a new string and does NOT modify the existing instance. Remember: .Net strings are ALMOST never modified in-place.

What you want is more like this:


text = text.Remove(.....

Share this post


Link to post
Share on other sites
Yes that was what was wrong

text = text.Remove(text.Length - 1);

worked. :)

Thanks:
Nypyren
and JWalsh (your NeHe conversions is what got me into XNA)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!