简体   繁体   English

重写/更改.NET Winforms标签控件的字体垂直对齐行为

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

I have a regular label with the TextAlign property set to MiddleCenter. 我有一个常规标签,其TextAlign属性设置为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. 据我了解,我认为.NET在技术上是正确的-从字体的意义上来说,垂直对齐是正确的,因为诸如字体上升和下降之类的事情。 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. 我仅使用数字和冒号,因此我对如果包含其他字符(例如“ g”)的外观不感兴趣,例如“ g”显然会使像素悬挂在基线以下。

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. 我尝试调整填充以强制执行此行为,但这只是导致文本被裁剪,.NET不允许我使用负填充/边距。 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. 我还尝试手动编辑ttf字体文件并更改字体上升/下降,以便.NET对其进行不同的对齐,但无法弄清楚。 Thanks for your advice. 谢谢你的建议。

The font file I am using was downloaded here: http://www.fontspace.com/style-7/digital-7 我正在使用的字体文件已在此处下载: 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: 在Form ResizeEnd事件上尝试以下操作:

    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 我修改了这个: https : //stackoverflow.com/a/9645577/1027551

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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