Click here to Skip to main content
15,902,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Updated 3-Aug-16 12:41pm
Sergey Alexandrovich Kryukov 26-Aug-13 23:45pm    
It looks like a bad idea to me. Why? If you use this approach, the font size will depend on the size you use for the instance of your control where you use it, but other controls don't behave this way.

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.

Share this answer
Mr.TMG 27-Aug-13 22:22pm    
I completely agree that this is more trouble than it's worth and will take more time than I have. Having said that I'm updating my question to asking advice.
Sergey Alexandrovich Kryukov 27-Aug-13 23:36pm    
My answer is this: why not doing the opposite: changing the size of the parent control to fit the string nicely? In WPF, this is defined by the window property System.Windows.Window.SizeToContent. In Forms, see System.Windows.Forms.TextRenderer.
Mr.TMG 28-Aug-13 1:07am    
Because the size of the parent control is defined by it's surroundings. For example: One of my controls displays a task - a task is a service that is preformed on an item (Sample). Samples are contained inside of a Job. I have a window that acts as a pop up like you would see on a combo box. That popup is populated with these controls that i'm having trouble with (Task Display). The combo-box look alike acts much like a combo-box but the textbox portion of it is replaced by a label the states the Name of the service and the number of tasks contained in the popup. The combo-box lookalike is then re sized to math the length of that label. Thus, the task display boxes contained in the list have significantly less room.

The task display box in the popup would say something along the lines of
Spark OES on Sample M13-235-01-A
where the combobox Label would say
Spark OES x6

Granted i did give myself some wiggle room so the amount of space you have is actually 2x the size of the combobox label in this case.

The combobox controls are populated horizontally across a JobLine (about the size of your web browser address bar.

The Job lines are populated vertically in a window.

Does anyone know how to post a photo on here?
Mr.TMG 28-Aug-13 1:24am    
here is a photo!1177&v=3
Sergey Alexandrovich Kryukov 28-Aug-13 1:44am    
So what? You still can have everything bigger according to some text size, and, when the overall size reaches the screen size, introduce scrolling.

Anyway, the selection of font is the worst of possible solution. Bigger texts won't be readable. After all, why not scrolling? This is a very common problem which cannot be solved "perfectly" by definition. There are many designs, and decreasing the text is not used. Or leave it to the user.

In all cases, review your design thoroughly. It seems to me that you are very much preoccupied with your current design. I would advise you to overcome this preoccupation. This is how design problems are really solved: by going beyond.

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.
Share this answer
Mr.TMG 27-Aug-13 22:19pm    
OUCH... I accept that this sounds like a definite solution and will probably try this one day just for the sake of doing it. Nut not on my current project though. Thanks for the info.
Sergey Alexandrovich Kryukov 15-Sep-13 11:31am    
I agree and voted 5, but with some notes:
I used MeasureString, but it is notoriously inaccurate, which is explained by such complications as font hinting.
There is more precise method of measuring which I found on some forum, but I found it was also not 100% accurate. In all cases, the actual string size may be slightly more then found. So, I had to add some extra field around the string, which is not 100% reliable approach.
Dave Kreskowiak 15-Sep-13 14:32pm    
Yeah, I found the same problem. I solved it by making an artificial "margin" inside the control of a few pixels on all sides. Also, the correct combination of StringFormat options minimizes the problem considerably, but doesn't completely eliminate it.

Now, if I could just find the code that I used back then.... ;)
Sergey Alexandrovich Kryukov 15-Sep-13 14:47pm    
I'm also not sure if I can find the source code or a link...
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

    If CurSize >= 0 Then
        lblQueue.Font = New Font(lblQueue.Font.Name, CurSize)
    End If
End Sub
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900