سؤال

Is there a way to draw a long text on the screen using SpriteBatch.DrawString? I mean inserts new lines when comes to the end of the screen.

هل كانت مفيدة؟

المحلول

I'd suggest looking at XNAwiki - TextRendering.

نصائح أخرى

Inserting new lines is something you'd have to calculate yourself it's not something you an do automatically with the XNA framework, but yes it's possible to write code to do that.

One way to do it would be to take the string want to write and to move through it a word at a time an measure it until you get enough words to fill the width of whatever area it is you want to fill. Once you've found that width, you'd either change the Y Position of the string you want to draw and move onto the next line or insert a new line character into the string at that point and start calculating the amount of words that should be on the next line.

A thing to pay attention to is that string manipulation is expensive and generates a lot of garbage so you should try and minimize the amount of times you do something like that. If the text is static and never changes it would be ideal to do this one and never again while the game is running.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top