How do I implement word wrap?

人走茶凉 提交于 2019-12-01 08:20:59

I found following code: XNA - Basic Word Wrapping

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

To add to Alina's answer, here is an extended version of that function, that will also linebreak single words that are longer than 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();
    }

To handle a block of text with carriage returns you need to modify the code as below:

  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();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!