如何拆分长字符串以使其与给定的矩形匹配? [英] How can I split a long string so that it matches with a given rectangle?

查看:27
本文介绍了如何拆分长字符串以使其与给定的矩形匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长的字符串,我想把它分成几部分,这样文本的每一行总是在给定的矩形中.文本不应超出矩形的边框.

I have one long string and I want to split it in a few pieces so that every line of the text is always in the given rectangle. The text should not exceed the border of the rectangle.

矩形的高度不是问题.文本永远不会触及矩形的底部,因为矩形非常高.但是矩形不是很宽.

The height of the rectangle isn't the problem. The text will never touch the bottom of the rectangle because the rectangle is very tall. But the rectangle isn't very wide.

如何计算每行应绘制字符串的哪些部分?我不想分裂一个词.如果一个词超出了矩形的边界,那么这个词应该画在下一行.

How can I calculate which parts of the string should be drawn in each line? I don't want to split a word. If a word exceeds the border of the rectangle, then the word should be drawn in the next line.

例如,绘制字符串应如下所示:

For example, drawing the string should look like this:

Cyberpunk 2077 is an upcoming role-playing video game
developed and published by CD Projekt, releasing for
Google Stadia, Microsoft Windows, PlayStation 4, and
...

而不是那样:

Cyberpunk 2077 is an upcoming role-playing video game devel
oped and published by CD Projekt, releasing for Google Stad
ia, Microsoft Windows, PlayStation 4, and Xbox One on 16
...

现在,spriteBatch 正在像这样在一行中绘制完整的长字符串,并且文本超出了矩形的边界.如何将其正确拆分为几行?

Right now, spriteBatch is drawing the full long string like that in just one line and the text exceeds the border of the rectangle. How can I split it up correctly in a few lines?

Cyberpunk 2077 is an upcoming role-playing video game developed and published by CD Projekt, releasing for Google Stadia, Microsoft Windows, PlayStation 4, and Xbox One on 16 April 2020. Adapted from the 1988 tabletop game Cyberpunk 2020, it is set fifty-seven years later in dystopian Night City, an open world with six distinct regions. In a first-person perspective, players assume the role of the customisable mercenary V, who can reach prominence in hacking, machinery, and combat. V has an arsenal of ranged weapons and options for melee combat.

我使用 spriteBatch.DrawString 绘制字符串:

I use spriteBatch.DrawString to draw the string:

string Text = "Cyberpunk 2077 is an upcoming role-playing video game developed and published by CD Projekt, releasing for Google Stadia, Microsoft Windows, PlayStation 4, and Xbox One on 16 April 2020. Adapted from the 1988 tabletop game Cyberpunk 2020, it is set fifty-seven years later in dystopian Night City, an open world with six distinct regions. In a first-person perspective, players assume the role of the customisable mercenary V, who can reach prominence in hacking, machinery, and combat. V has an arsenal of ranged weapons and options for melee combat.";

protected override void Draw(GameTime gameTime)
{                
    graphics.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);
    spriteBatch.Begin();
    spriteBatch.DrawString(Font, Text, new Vector2(200, 300), Microsoft.Xna.Framework.Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0f);
    spriteBatch.End();

    base.Draw(gameTime);
}

更新:你需要在最后添加这行代码:

UPDATE: You need to add this line of code at the end:

收益返回缓冲区;

    string[] lines = Split(Text, 400, Font).ToArray();

    public static IEnumerable<string> Split(string text, double rectangleWidth, SpriteFont font)
    {
        var words = text.Split(' ');
        string buffer = string.Empty;

        foreach (var word in words)
        {
            var newBuffer = buffer + " " + word;
            if (word == words[0])
              newBuffer = word;
            else
              newBuffer = buffer + " " + word;

            Vector2 FontMeasurements = font.MeasureString(newBuffer);

            if (FontMeasurements.X >= rectangleWidth)
            {
                yield return buffer;
                buffer = word;
            }
            else
            {
                buffer = newBuffer;
            }
        }
        yield return buffer;
    }

绘图:

 for (int i = 0; i <= lines.Count() - 1; i++)
   spriteBatch.DrawString(Font, lines[i], new Vector2(300, 500 + i * 30), Microsoft.Xna.Framework.Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0f);

或:

string boxedText = string.Join('\n', Split(Text, 400, Font));
spriteBatch.DrawString(Font, boxedText, new Vector2(300, 500), Microsoft.Xna.Framework.Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0f);

推荐答案

假设您有一个方法 Measure(string text) 返回文本的实际视觉宽度(如果您使用等宽字体),您可以使用此方法将文本分成几行:

Assuming you have a Method Measure(string text) which returns the actual visual width of the text (not needed if you use a monospaced font), you could use this method to split the text into lines:

public static IEnumerable<string> Split(string text, double rectangleWidth)
{
    var words = text.Split(' ');
    string buffer = string.Empty;

    foreach (var word in words)
    {
       var newBuffer = buffer + " " + word;

       if (Measure(newBuffer) >= rectangleWidth)
       {
           yield return buffer;
           buffer = word;
       }
       else
       {
           buffer = newBuffer;
       }
    }

    yield return buffer;
}

要将行作为数组获取,请使用 string[] lines = Split(text, Rectangle.Width).ToArray().

To get the lines as an array, use string[] lines = Split(text, Rectangle.Width).ToArray().

如果你想要一个由换行符分隔的单个字符串,使用 string boxedText = string.Join('\n', Split(text, Rectangle.Width)).

If you want a single string separated by newlines, use string boxedText = string.Join('\n', Split(text, Rectangle.Width)).

在你的情况下,你会像这样使用它:

In your case you would use it like this:

string Text = "Cyberpunk 2077 is an upcoming role-playing video game developed and published by CD Projekt, releasing for Google Stadia, Microsoft Windows, PlayStation 4, and Xbox One on 16 April 2020. Adapted from the 1988 tabletop game Cyberpunk 2020, it is set fifty-seven years later in dystopian Night City, an open world with six distinct regions. In a first-person perspective, players assume the role of the customisable mercenary V, who can reach prominence in hacking, machinery, and combat. V has an arsenal of ranged weapons and options for melee combat.";

protected override void Draw(GameTime gameTime)
{                
    graphics.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);
    spriteBatch.Begin();
    spriteBatch.DrawString(
        Font, 
        string.Join(' ', Split(Text, Rectangle.Width)), 
        new Vector2(200, 300), 
        Microsoft.Xna.Framework.Color.White, 
        0, 
        Vector2.Zero,
        1f,
        SpriteEffects.None,
        0f);
    spriteBatch.End();

    base.Draw(gameTime);
}

这篇关于如何拆分长字符串以使其与给定的矩形匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆