Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the position of the text baseline in a Label and a NumericUpDown?

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?

like image 969
Simon Avatar asked Jun 17 '09 09:06

Simon


1 Answers

// 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);
like image 124
dar7yl Avatar answered Oct 04 '22 17:10

dar7yl