I'm trying to create coloumns of text for a C# app that will be running on MSN Messenger. I'm having trouple getting everything to line up.
Here's an example of the output that I want:
1) Pizza Hut 123 Fake St.
2) Domino's Pizza 123 Fake St.
3) The Cheesecake Cafe 123 Fake St.
4) Capital Pizza & Steakhouse 123 Fake St.
5) Funky Pickle Pizza 123 Fake St.
6) Boston Pizza 123 Fake St.
7) Rose Bowl Rouge Lounge 123 Fake St.
8) Royal Pizza 123 Fake St.
9) A Columbus Pizza & Donair Inc 123 Fake St.
But because it is a variable width font it is displaying like this:
1) Pizza Hut 123 Fake St.
2) Domino's Pizza 123 Fake St.
3) The Cheesecake Cafe 123 Fake St.
4) Capital Pizza & Steakhouse 123 Fake St.
5) Funky Pickle Pizza 123 Fake St.
6) Boston Pizza 123 Fake St.
7) Rose Bowl Rouge Lounge 123 Fake St.
8) Royal Pizza 123 Fake St.
9) A Columbus Pizza & Donair Inc 123 Fake St.
I have tried using the C# string.PadRight() function as well as creating my own function that add padding using spaces and tabs. Both work fine with fixed width fonts but break with variable width fonts.
Is there any way to determine the width of a string in a given font?
Or does anyone have any other suggestions?
Just use the TextRenderer class. The simplest case :
Size size = TextRenderer.MeasureText("Hello world", someFont);
If you don't have access to System.Windows.Fonts Graphics.MeasureString remains, it have some limitations but should do the work :
Bitmap bmp = new Bitmap(1,1);
Graphics graphics = Graphics.FromImage(bmp);
SizeF size = graphics.MeasureString("Hello world", someFont);
But be aware that if the font of your text and the spaces MUST be the same there will be cases where you can't align the text perfectly. I don't know what MSN Messenger is able to do in your case but except if you have access to at least a subset of HTML you won't have a perfect output.
You should also be aware that if you do measurements on a local computer and send to another without the correct font installed your columns won't look like columns anymore so your are limited to the basic subset of fonts presents on all computers.
If multiple operating system support is also a requirement you will have some big problems as the Arial font on Mac and PCs doesn't look (and measure) exactly the same.