I have an array of classes that contains some text and a font. I want to draw all the text aligned on line independently on font size. I thought I can just subtract font height from Y-positiontion of the line and draw the text on the new position but this is a bit difficult because of top and bottom padding added to the text by GDI. The calculation works right, but the text floats somewhere in the middle of rectangle it should be in. I also found out that I can set TextFormatFlags
to NoPadding
but this helps just for left and right padding and the text still floats above the line. I was searching a long time for a way to get rid of all padding, but I didn't found anything that could do this.
This is my code:
public static void DrawTextOnLine(string text, Font font, Graphics graphics, Color color, int x, int dLineY)
{
float upperY = dLineY - GetFontAscent(font);
Point point = new Point(x, (int)upperY);
TextRenderer.DrawText(graphics, text, font, point, color, TextFormatFlags.NoPadding);
}
One thing I forgot to mention: I also tried to do it with TextFormatFlags
set to Bottom
. The problem with this is that descending letters are causing the text to be above the line.
Is there an easier way to do it or how can I remove all the padding?