Graphics.DrawString() & leading padding spaces for Proportional Font
I've been using a monospaced font to render a table onto a graphics object, and to align the columns of text I'd been padding the text using space characters.
I've been asked to change the font to a proportional font, and no surprise, but the columns don't line up anymore.
If given a string such as
"Bill-of-Material Edits\r\n------------------------------\r\n200 510024 Door 24\" x 58\"\r\n 3 530058 Panel 58\" x 58\"\r\n";
how do I ensure that the 3 lines up properly under the one's place on the 200 value from the line above, and subsequently have the 510024 sit directly above the 530058?
Here's the code I use to draw the string:
var fnt = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular, GraphicsUnit.Point);
StringFormat strFormat = new StringFormat(StringFormat.GenericTypographic);
开发者_运维知识库 strFormat.Alignment = StringAlignment.Near;
string text = "Bill-of-Material Edits\r\n------------------------------\r\n200 510024 Door 24\" x 58\"\r\n 3 530058 Panel 58\" x 58\"\r\n";
g.DrawString(text, fnt, Brushes.Black, new RectangleF(10f, 10f, 38.1062851f, 12.9231777f), strFormat);
I tried replacing the spaces with other characters such as unicode control characters (0x0080) to no avail. I've also tried using string.Format() with formatters like {0,10} which didn't help either.
What do I do to get my columns to line up?
You can't make it work like this. Draw each individual string in its column, pass the Rectangle of the column. How wide you make each column is up to you.
精彩评论