Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word wrap a string in multiple lines

Tags:

c#

word-wrap

I am trying to word wrap a string into multiple lines. Every line will have a defined width.

For example, I would get this result if I word wrap it to an area of 120 pixels in width:

Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Sed augue
velit, tempor non vulputate sit amet,
dictum vitae lacus. In vitae ante
justo, ut accumsan sem. Donec
pulvinar, nisi nec sagittis consequat,
sem orci luctus velit, sed elementum
ligula ante nec neque. Pellentesque
habitant morbi tristique senectus et
netus et malesuada fames ac turpis
egestas. Etiam erat est, pellentesque
eget tincidunt ut, egestas in ante.
Nulla vitae vulputate velit. Proin in
congue neque. Cras rutrum sodales
sapien, ut convallis erat auctor vel.
Duis ultricies pharetra dui, sagittis
varius mauris tristique a. Nam ut
neque id risus tempor hendrerit.
Maecenas ut lacus nunc. Nulla
fermentum ornare rhoncus. Nulla
gravida vestibulum odio, vel commodo
magna condimentum quis. Quisque
sollicitudin blandit mi, non varius
libero lobortis eu. Vestibulum eu
turpis massa, id tincidunt orci.
Curabitur pellentesque urna non risus
adipiscing facilisis. Mauris vel
accumsan purus. Proin quis enim nec
sem tempor vestibulum ac vitae augue.

like image 702
ForrestWhy Avatar asked Oct 18 '10 16:10

ForrestWhy


People also ask

How do I insert multiple lines of text in word?

Step 1. Press the "Hyphen (-)" key three times then press "Enter" to place a solid line across the page. Hold down the "Shift" key and press the "Underline ( _ )" key three times. Release both keys then press "Enter" to place a heavier line across the Word document.

How do I wrap text around a paragraph in word?

Go to Picture Format or Shape Format and select Arrange > Wrap Text. If the window is wide enough, Word displays Wrap Text directly on the Picture Format tab. Choose the wrapping options that you want to apply. For example, In Line with Text, Top and Bottom, and Behind Text.

How do you wrap text in word list?

You can access Text Wrapping options in three ways: With the picture selected, click the contextual Format tab and then click the Wrap Text option in the Arrange group. Right-click the picture and choose Text Wrapping from the resulting submenu. Select the picture and then click the Layout Options tip to the right.


1 Answers

static void Main(string[] args) {     List<string> lines = WrapText("Add some text", 300, "Calibri", 11);      foreach (var item in lines)     {         Console.WriteLine(item);     }      Console.ReadLine(); }  static List<string> WrapText(string text, double pixels, string fontFamily,      float emSize) {     string[] originalLines = text.Split(new string[] { " " },          StringSplitOptions.None);      List<string> wrappedLines = new List<string>();      StringBuilder actualLine = new StringBuilder();     double actualWidth = 0;      foreach (var item in originalLines)     {         FormattedText formatted = new FormattedText(item,              CultureInfo.CurrentCulture,              System.Windows.FlowDirection.LeftToRight,             new Typeface(fontFamily), emSize, Brushes.Black);          actualLine.Append(item + " ");         actualWidth += formatted.Width;          if (actualWidth > pixels)         {             wrappedLines.Add(actualLine.ToString());             actualLine.Clear();             actualWidth = 0;         }     }      if(actualLine.Length > 0)         wrappedLines.Add(actualLine.ToString());      return wrappedLines; } 

Add WindowsBase and PresentationCore libraries.

like image 165
as-cii Avatar answered Oct 11 '22 12:10

as-cii