我正在尝试在monogame窗口中进行应用。我有一个长文本要显示在屏幕上。我尝试使用spriteBatch.Drawstring在屏幕上渲染它,在某种程度上是成功的。但是,该文本不适合所需的区域。我跟着this教程。我需要实现一个垂直滚动,以便将整个文本放在我想要的区域内。有人可以建议一些帮助。这是我目前的代码:
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
_boxTexture = new SolidColorTexture(GraphicsDevice, Color.Red);
_borderRectangle = new Rectangle(100, 100, 500, 500);
_textboxRectangle = new Rectangle(105, 105, 490, 490);
_font = Content.Load<SpriteFont>("Rockwell");
_text = "He determined to drop his litigation with the monastry, and relinguish his claims to the wood-cuting and fishery rihgts at once. He was the more ready to do this becuase the rights had becom much less valuable, and he had indeed the vaguest idea where the wood and river in quedtion were.";
}
private String parseText(String text)
{
String line = String.Empty;
String returnString = String.Empty;
String[] wordArray = text.Split(' ');
foreach (String word in wordArray)
{
if (font.MeasureString(line + word).Length() > textBox.Width)
{
returnString = returnString + line + '\n';
line = String.Empty;
}
line = line + word + ' ';
}
return returnString + line;
}
和内部绘制功能:
spriteBatch.DrawString(font, parseText(text), new Vector2(textBox.X, textBox.Y), Color.White);
答案 0 :(得分:0)
您可以在draw方法中执行此操作。 然后就做你现在正在做的事情,但不是创建你返回的字符串,而是打电话
spriteBatch.DrawString(font, line, textPostion, Color.White);
代替。其中textPosition恰好等于文本框位置,首先使用font.MeasureString(line).Y为每次迭代增加Y位置:
textPosition.Y += font.MeasureString(line).Y;
然后你检查
if(font.MeasureString(line).Y + textPosition.Y < textBox.Y + textBox.Height
|| textPosition.Y > textBox.Y)
{
continue;
}
然后只需查看键盘箭头的输入(或创建一些向上和向下滚动的按钮),并相应地增大或减小textPosition.Y。然后你将有垂直滚动文本框。
然后,您可以通过为位置定义最小Y值来进行锁定,以便在滚动到底部或顶部时文本停止。