简体   繁体   中英

Override/Change .NET Winforms Font Vertical Alignment Behavior for Label Control

I have a regular label with the TextAlign property set to MiddleCenter. The label is intended to hold a time value with minutes and seconds and will function as a stopwatch.

If the font size is small the label is vertically aligned correctly. However, as the font size increases the vertical alignment becomes problematic...

See screenshot here and you will see what I mean:

屏幕截图

As far as I understand, I think .NET is technically correct - I think the vertical alignment is correct from a typographical sense because of things like font ascent and descent. However this doesn't really fit my needs in this case. In the screenshot the top portion of the label has more space than bottom portion of the label. I would like these two portions to be equal when the numbers are displayed. I'm only using numbers and the colon, so I'm not interested in how it might look if other characters were included such as a "g" which obviously would have pixels hanging below the baseline.

I tried tweaking the padding to force this behavior but it just resulted in the text being clipped and .NET won't let me use negative padding/margin. Is there any easy way to accomplish what I want here? I also tried to manually edit the ttf font file and change the font ascent / descent so that .NET aligns it differently but was not able to figure it out. Thanks for your advice.

The font file I am using was downloaded here: http://www.fontspace.com/style-7/digital-7

it appears that if the ratio is to large, the text just "sinks" and eventually just gets cut off.

Try this on the Form ResizeEnd event:

    label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    //First make the font big enough
    double fontSize = label1.Width / label1.Text.Count();
    int height = label1.Height;
    fontSize = fontSize > 0 ? (double)fontSize : 1;
    if (fontSize < (height / 2))
    {
        fontSize = (height / 2);
    }
    label1.Font = new System.Drawing.Font(label1.Font.FontFamily, (float)fontSize, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

    //then adjust the text to the label size
    while (label1.Width < System.Windows.Forms.TextRenderer.MeasureText(label1.Text,
        new Font(label1.Font.FontFamily, label1.Font.Size, label1.Font.Style)).Width ||
        label1.Height < System.Windows.Forms.TextRenderer.MeasureText(label1.Text,
        new Font(label1.Font.FontFamily, label1.Font.Size, label1.Font.Style)).Height)
    {
        label1.Font = new Font(label1.Font.FontFamily, label1.Font.Size > 1 ? label1.Font.Size - 0.5f : label1.Font.Size, label1.Font.Style);
        if (label1.ClientRectangle.Width < 3 || label1.ClientRectangle.Height < 3)
            break;
    }

I modified this: https://stackoverflow.com/a/9645577/1027551

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM