Click here to Skip to main content
14,390,263 members
Rate this:
Please Sign up or sign in to vote.
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.
Posted
Updated 3-Aug-16 13:41pm
v2
Comments
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.
—SA
Rate this:
Please Sign up or sign in to vote.

Solution 1

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
   
Comments
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.
—SA
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 https://skydrive.live.com/redir?resid=1A24B4A8C01B42BB!1177&v=3
   
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.

—SA
Mr.TMG 28-Aug-13 20:11pm
   
Thanks for taking the time chat about it with me. I was talking to some guys at work and came up with a nice looking work around that involves popping the display out and expanding it to the label size when the mouse enters and setting it back into place on mouse leave. Also removed some unnecessary length from the text.
Thanks again.
Sergey Alexandrovich Kryukov 28-Aug-13 22:18pm
   
You are very welcome.
Good luck, call again.
—SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
   
Comments
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.
—SA
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...
—SA
Rate this:
Please Sign up or sign in to vote.

Solution 3

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
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100