4

XNA 有 Spritefont 类,它有一个 MeasureString 方法,可以返回 Width 和 Height 的值string。我试图了解如何创建一种方法,该方法将有效地返回Environment.Newline插入在正确位置的字符串,以便如果适合某个宽度和高度(矩形用作该参数)。

4

3 回答 3

7

我发现以下代码:XNA - 基本自动换行

public string WrapText(SpriteFont spriteFont, string text, float maxLineWidth)
{
    string[] words = text.Split(' ');
    StringBuilder sb = new StringBuilder();
    float lineWidth = 0f;
    float spaceWidth = spriteFont.MeasureString(" ").X;

    foreach (string word in words)
    {
        Vector2 size = spriteFont.MeasureString(word);

        if (lineWidth + size.X < maxLineWidth)
        {
            sb.Append(word + " ");
            lineWidth += size.X + spaceWidth;
        }
        else
        {
            sb.Append("\n" + word + " ");
            lineWidth = size.X + spaceWidth;
        }
    }

    return sb.ToString();
}
于 2013-04-13T12:03:11.113 回答
2

为了添加到 Alina 的答案,这里是该函数的扩展版本,它也将换行比 maxLineWidth 长的单个单词

    public static string WrapText(SpriteFont font, string text, float maxLineWidth)
    {
        string[] words = text.Split(' ');
        StringBuilder sb = new StringBuilder();
        float lineWidth = 0f;
        float spaceWidth = font.MeasureString(" ").X;

        foreach (string word in words)
        {
            Vector2 size = font.MeasureString(word);

            if (lineWidth + size.X < maxLineWidth)
            {
                sb.Append(word + " ");
                lineWidth += size.X + spaceWidth;
            }
            else
            {
                if (size.X > maxLineWidth)
                {
                    if (sb.ToString() == "")
                    {
                        sb.Append(WrapText(font, word.Insert(word.Length / 2, " ") + " ", maxLineWidth));
                    }
                    else
                    {
                        sb.Append("\n" + WrapText(font, word.Insert(word.Length / 2, " ") + " ", maxLineWidth));
                    }
                }
                else
                {
                    sb.Append("\n" + word + " ");
                    lineWidth = size.X + spaceWidth;
                }
            }
        }

        return sb.ToString();
    }
于 2015-12-26T18:01:41.397 回答
1

要处理带有回车的文本块,您需要修改代码如下:

  public static string WrapText(SpriteFont font, string text, float maxLineWidth)
    {
        string[] words = text.Split(' ');
        StringBuilder sb = new StringBuilder();
        float lineWidth = 0f;
        float spaceWidth = font.MeasureString(" ").X;

        foreach (string word in words)
        {
            Vector2 size = font.MeasureString(word);

            if (word.Contains("\r"))
            {
                lineWidth = 0f;
                sb.Append("\r \r" );
            }

            if (lineWidth + size.X < maxLineWidth )
            {
                sb.Append(word + " ");
                lineWidth += size.X + spaceWidth;
            }

            else
            {
                if (size.X > maxLineWidth )
                {
                    if (sb.ToString() == " ")
                    {
                        sb.Append(WrapText(font, word.Insert(word.Length / 2, " ") + " ", maxLineWidth));
                    }
                    else
                    {
                        sb.Append("\n" + WrapText(font, word.Insert(word.Length / 2, " ") + " ", maxLineWidth));
                    }
                }
                else
                {
                    sb.Append("\n" + word + " ");
                    lineWidth = size.X + spaceWidth;
                }
            }
        }

        return sb.ToString();
    }
于 2016-09-06T12:38:26.230 回答