Here is the code to wrap multi-line text without cutting words:
private void Form1_Load(object sender, EventArgs e)
{
string sss = "Farshid Ariashokooh Arvin Rose Software Gorup. www.arvin-rose.com";
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 (TextRenderer.MeasureText(_str, _font).Width +5 < (_size))
return _str;
int p1 = 0, p2 = 0 ;
for (int i = 0; i < _str.Length; i++)
{
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) != " ")
{
y--;
count++;
if (y == 0 || y <= p1)
{
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;
}