调整字体大小以适合用户控制 [英] Adjust font size to fit user control

查看:260
本文介绍了调整字体大小以适合用户控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个旨在显示名称的用户控件.控件内部有一个标签,该标签应始终居中并可以具有未知长度的文本.

是否有任何巧妙的方法来调整标签的字体大小,以使标签尽可能多地填充用户控件,同时将整个文本保持在控件的范围内?如果是这样,有人会向我指出该方法吗?



新问题.

关于将大文本字符串放入小框内的想法,这些小框将大量出现在屏幕上,同时看起来不错"吗?
最好快速编码.

如果可以弄清楚如何发布图片,我将向您展示我的工作方式.

Hi,
I have a user control that is designed to display Names. Inside the control there is a label that should always remain centered and can have an unknown length of text.

Are there any clever methods out there for adjusting the font size of the label such that it fills the user control as much as possible while keeping the entirety of the text within the bounds of the control? If so, will someone point that method out to me please?



New question.

Any ideas on fitting large strings of text inside of small boxes that will appear in large quantity on the screen and, at the same time, look "nice"?
preferably fast to code.

If I can figure out how to post pictures I''ll show you what I''m working with.

推荐答案

请参阅我对问题的评论.即使实现了想要的行为(由于字体提示,这本身也非常困难),但您可能会遇到与UI的其他元素非常不愉快的风格冲突.

相反,您应该查看UI设计.

—SA
Please see my comment to the question. Even if you implement the behavior you want (which is itself surprisingly difficult, because of font hinting), you may run into a very unpleasant stylistic clash with other elements of your UI.

Rather, you should review your UI design.

—SA


因此,您希望字体自动调整大小以适合标签中的字符串吗?该方法不存在.您必须自己编写代码.

我这样做是(很久以前!),方法是创建自己的控件(继承自Panel,而不继承Label),然后自己绘制文本.

您需要使用 MeasureString [
So you want the font to resize itself to fit the string in a label?? The method doesn''t exist. You''ll have to code this up yourself.

I did this (a long time ago!) by creating my own control, inherited from Panel, not Label, and drawing the text myself.

You''ll need to use MeasureString[^] and the StringFormat options to tell you how big the box is that can contain the drawn string. The font size you pass to MeasureString would be fixed and not ever change. In my code I think I used like 72 points.

You then take the returned size and create a Bitmap with it, then draw your string to the bitmap using the exact same options you passed to MeasureString. When you draw your text, draw the bitmap image (NOT THE STRING!) to your control surface and scale it down to the size you need. Since you''re now drawing a picture of your text, and scaling it down, this will, of course, wreck the nice anti-aliased edges of the glyphs drawn.


我明白了无需使用面板,而只需使用标签即可工作.当文本更改或标签大小更改时,我将其称为代码(我将其设置为在所有方向锚定).就我而言,文本只是一个数字,每秒变化一次,以实现近乎实时的视觉更新.我希望这可以帮助下一个想知道如何完成此工作的人:

I got this working without using a panel, just a label. I call this code when the text changes or the size of the label changes (I have it set to anchor on all sides). In my case, the text is simply a number that changes every second for near real time visual updates. I hope this helps the next person wondering how this is done:

Private Sub AdjustText()
    Dim Fit As Boolean = False
    Dim CurSize As Single
    Dim SizeStep As Single = 1
    Do Until Fit
        CurSize += SizeStep
        Dim Fnt As Font = New Font(lblQueue.Font.Name, CurSize)
        Dim textSize As Size = TextRenderer.MeasureText(lblQueue.Text, Fnt)
          If textSize.Height >= lblQueue.Height Or textSize.Width >= lblQueue.Width Or lblQueue.Height = 0 Or lblQueue.Width = 0 Then
            Fit = True
            If textSize.Width > lblQueue.Width Then
                CurSize -= SizeStep
            End If
            If textSize.Height > lblQueue.Height Then
                CurSize -= SizeStep
            End If
        End If
    Loop

    If CurSize >= 0 Then
        lblQueue.Font = New Font(lblQueue.Font.Name, CurSize)
        Application.DoEvents()
    End If
End Sub


这篇关于调整字体大小以适合用户控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆