How to get the distance between the baseline of text and the horizontal border of a label?

不问归期 提交于 2021-02-19 04:07:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!