There is a message for the forum that the user sent. The field for the message on width and height is limited. How to break a message so that it fit the width of this field and, if it does not fit in height, then put an ellipsis at the end of the message? So that half of the word was on one line, and the other half on the second can NOT be split.

JavaScript and C # implementation is desirable.

  • height is the number of lines, width is the number of characters !!! - kkkod

1 answer 1

To determine the space occupied by text, use a Graphics.MeasureString . And breaks or not - you can determine by breaking the line into words and adding one at a time. As soon as the word has not got - we begin with it a new line. As soon as I didn’t fit in the number of lines, we put ellipsis instead of the last word of the last line. Then the resulting lines are merged again into one and display the resulting paragraph.