Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Word wrap without cutting words

, 4 Jun 2012
Rate this:
Please Sign up or sign in to vote.
Wrap multi-line text without cutting words.

Here is the code to wrap multi-line text without cutting words:

private void Form1_Load(object sender, EventArgs e)
{
    // String
    string sss = "Farshid Ariashokooh Arvin Rose Software Gorup. www.arvin-rose.com";
    // Return The String with Font And Size
    string Ret = lineStringAnalys(sss, 160, new Font("Arial", 10, FontStyle.Bold));
    MessageBox.Show(Ret);                        
    this.Close();

}
private string lineStringAnalys(string _str, int _size, Font _font)
{
   string NewStr =_str;
   // If do not need to Check 
    if (TextRenderer.MeasureText(_str, _font).Width +5 < (_size))
        return _str; // Retun 
    int p1 = 0, p2 = 0 ;
    for (int i = 0; i < _str.Length; i++) // Check char by char the string 
    {
        p2++;
        try
        {
            if (p1 + p2 > _str.Length)
                break;
            if (TextRenderer.MeasureText(_str.Substring(p1, p2), _font).Width + 5 >= (_size))
            {
                int aaa = TextRenderer.MeasureText(_str.Substring(p1, p2), _font).Width;
                string bbbb = _str.Substring(p1, p2);
                int y = i, count = 0;
                while (_str.Substring(y, 1) != " ") // look for the last word
                {
                    y--;
                    count++;
                    if (y == 0 || y <= p1) // if not found word
                    {
                        count = 0;
                        break;
                    }

                }
                p2 -= count;
                NewStr = NewStr.Insert(p1 + p2, "\n");
                p1 += p2 + 1;
                
                p2 = 0;
                i -= count;
            }
        }
        catch
        {
            MessageBox.Show(i.ToString());
        }


    }
    return NewStr;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Farshid Ariashokooh
Team Leader Arvin Rose
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
i can do anything

Comments and Discussions

 
GeneralNeeds a few more details... PinmemberMatt T Heffron4-Jun-12 12:17 
GeneralRe: Needs a few more details... PinmemberFarshid Ariashokooh4-Jun-12 18:29 
GeneralRe: Needs a few more details... PinmemberMatt T Heffron8-Jun-12 13:18 
GeneralThoughts PinmemberPIEBALDconsult4-Jun-12 11:16 
GeneralRe: Thoughts PinmemberFarshid Ariashokooh4-Jun-12 15:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 4 Jun 2012
Article Copyright 2012 by Farshid Ariashokooh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid