Advertisement
infinite_ammo

WrapText.cs

Jul 24th, 2014
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1.     string WrapText(string textToWrap, float width)
  2.     {
  3.         string returnText = "";
  4.         string line = "";
  5.         int lastSpace = 0;
  6.         for (int i = 0; i < textToWrap.Length; i++)
  7.         {
  8.             if (line.Length == 0 && textToWrap[i] == ' ')
  9.                 continue;
  10.            
  11.             line += textToWrap[i];
  12.             if (line[line.Length-1] == ' ')
  13.                 lastSpace = line.Length-1;
  14.            
  15.             textMesh.text = line;
  16.             //Debug.Log("width: " + textMesh.renderer.bounds.extents.x);
  17.             if (textMesh.renderer.bounds.extents.x * textMesh.transform.lossyScale.x > width)
  18.             {
  19.                 string leftOver = line.Substring (lastSpace+1, line.Length - (lastSpace+1));
  20.                 line = line.Substring(0, lastSpace);
  21.                 returnText += line + "\n";
  22.                 line = leftOver;
  23.                 lastSpace = 0;
  24.             }
  25.         }
  26.  
  27.         if (line != "")
  28.             returnText += line + "\n";
  29.         textMesh.text = "";
  30.  
  31.         return returnText;
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement