问题
I want to get the exact distance between the baseline of the text and the bottom border of a label in C#. I want this because I want to draw a line under the text (don't want to use underlined font, because it's so tight/close to the text).
Here is my try:
//This is placed in the custom label class
int dy = (int)((ClientRectangle.Height - Font.GetHeight())/2);
But it's not exact, the dy returns about 3 and the line drawn to the label is too far from the baseline of the text.
回答1:
To get text baseline for a label, assuming you are inside the custom label class, in the drawing handler.
Font myFont = this.Font;
FontFamily ff = myFont.FontFamily;
float lineSpace = ff.GetLineSpacing(myFont.Style);
float ascent = ff.GetCellAscent(myFont.Style);
float baseline = myFont.GetHeight(e.Graphics) * ascent / lineSpace;
Credit goes here.
来源:https://stackoverflow.com/questions/16083873/how-to-get-the-distance-between-the-baseline-of-text-and-the-horizontal-border-o