If you're already using the right bounding box, I'd write a function that simply injected '\n's.
overslacked
2009-10-05 16:49:37
If you're already using the right bounding box, I'd write a function that simply injected '\n's.
Yes, by using the escape character of \n (new line), it will force down. However, if you enter the \n within the visual designer, it will put text as \n thinking you meant to have the actual punctuation backslash character and not the escape character of newline.
Just split the string into characters and draw each one adding the line height of your font to your Y-axis variable:
protected override void OnPaint(PaintEventArgs e)
{
float x = 10.0F;
float y = 10.0F;
string drawString = "123";
using(SolidBrush brush = new SolidBrush(Color.Black))
using (Font drawFont = new Font("Arial", 16))
{
foreach (char c in drawString.ToCharArray())
{
PointF p = new PointF(x, y);
e.Graphics.DrawString(c.ToString(), drawFont, brush, p);
y += drawFont.Height;
}
}
base.OnPaint(e);
}
Here is an example project that does vertical Text. Also has some comments about text alignment.
From the example, you can use the StringAlignment.Center to center the characters and pass it to the last parameter of the DrawString call.
protected override void OnPaint(PaintEventArgs e)
{
float x = 10.0F;
float y = 10.0F;
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
foreach (char c in Text.ToCharArray())
{
PointF p = new PointF(x, y);
e.Graphics.DrawString(c.ToString(), drawFont, drawBrush, p, sf);
y += drawFont.Height;
}
}