Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphics DrawString with controlled Word Wrap

Tags:

Basically my issue is that I need to word wrap a string when I want to. Not when .NET wants to. I understand that the DrawString method will automatically word wrap if I give it a rectangle to draw within. I need to control when it does word wrap it. So lets say my string I want to draw is Testing 1234. And I want to draw text on a new line whenever I see a space. So in this case it would have two lines - Testing and 1234. I am guessing I need a combination of re-sizing the string (to fit my bounds) and multiple drawstring method calls to draw each line. The problem is that I don't really know how to do this. I am a novice when it comes to GDI+.

like image 769
Travyguy9 Avatar asked Feb 24 '10 20:02

Travyguy9


1 Answers

You could replace the spaces with newlines and then draw the string.

string converted = text.Replace(" ", System.Environment.NewLine);
like image 83
Zach Johnson Avatar answered Oct 11 '22 15:10

Zach Johnson