XNA 在屏幕上绘制文本
有没有办法使用 SpriteBatch.DrawString 在屏幕上绘制长文本?我的意思是当到达屏幕末尾时插入新行。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议查看XNAwiki - TextRendering。
I'd suggest looking at XNAwiki - TextRendering.
插入新行是您必须自己计算的事情,这不是您使用 XNA 框架自动执行的操作,但是可以编写代码来执行此操作。
一种方法是取出想要写入的字符串,一次移动一个单词并测量它,直到获得足够的单词来填充您想要填充的任何区域的宽度。找到该宽度后,您可以更改要绘制的字符串的 Y 位置并移动到下一行,或者在该点插入一个新行字符到字符串中,然后开始计算应该绘制的单词数。在下一行。
需要注意的一点是,字符串操作的成本很高,并且会产生大量垃圾,因此您应该尝试尽量减少执行此类操作的次数。如果文本是静态的并且永远不会改变,那么最好在游戏运行时执行此操作并且不再执行此操作。
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.