簡體   English   中英

如何拆分長字符串以使其與給定的矩形匹配?

[英]How can I split a long string so that it matches with a given rectangle?

我有一個長字符串,我想將它分成幾個部分,以便文本的每一行始終在給定的矩形中。 文本不應超過矩形的邊框。

矩形的高度不是問題。 文本永遠不會觸及矩形的底部,因為矩形非常高。 但矩形不是很寬。

如何計算每行中應繪制字符串的哪些部分? 我不想分開一個字。 如果單詞超出矩形的邊框,則應在下一行中繪制單詞。

例如,繪制字符串應如下所示:

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只在一行中繪制完整的長字符串,文本超出了矩形的邊框。 如何在幾行中正確拆分?

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來繪制字符串:

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);
}

更新:你需要在最后添加這行代碼:

收益率緩沖;

    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) ,它返回Measure(string text)的實際可視寬度(如果使用等寬字體則不需要),您可以使用此方法將文本拆分為行:

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()

如果您想要一個由換行符分隔的單個字符串,請使用string boxedText = string.Join('\\n', Split(text, Rectangle.Width))

在你的情況下你會像這樣使用它:

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM