Click here to Skip to main content
12,750,208 members (36,882 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.7K views
3 bookmarked
Posted 4 Jun 2012

Word wrap without cutting words

, 4 Jun 2012 CPOL
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

You may also be interested in...

Comments and Discussions

 
GeneralNeeds a few more details... Pin
Matt T Heffron4-Jun-12 13:17
memberMatt T Heffron4-Jun-12 13:17 
GeneralRe: Needs a few more details... Pin
Farshid Ariashokooh4-Jun-12 19:29
memberFarshid Ariashokooh4-Jun-12 19:29 
GeneralRe: Needs a few more details... Pin
Matt T Heffron8-Jun-12 14:18
memberMatt T Heffron8-Jun-12 14:18 
GeneralThoughts Pin
PIEBALDconsult4-Jun-12 12:16
memberPIEBALDconsult4-Jun-12 12:16 
GeneralRe: Thoughts Pin
Farshid Ariashokooh4-Jun-12 16:37
memberFarshid Ariashokooh4-Jun-12 16:37 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170215.1 | Last Updated 4 Jun 2012
Article Copyright 2012 by Farshid Ariashokooh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid