I'm trying to align a Label
and a NumericUpDown
by their text baselines. I'm doing it in code, rather than the designer. How do I get the position of the text baseline?
// to render text with baseline at coordinates (pt.X, pt.Y) :
Font myFont = Label1.Font;
FontFamily ff = myFont.FontFamily;
float lineSpace = ff.GetLineSpacing(myFont.Style);
float ascent = ff.GetCellAscent(myFont.Style);
float baseline = myFont.GetHeight(ev.Graphics) * ascent / lineSpace;
PointF renderPt = new PointF(pt.X, pt.Y - baseline));
ev.Graphics.DrawString("Render this string", myFont, textBrush, renderPt);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With