none
TextBlock : How to decrease the font size automatically to make all the text completely visible?

    Question

  • Hello,

    I need to display some text on a control with fixed size, and all text must be visible.

    I use a TextBlock control. It has a fixed size of 2 inches * 1 inch for example. If I set the text to "Hello World!" all text is visible. If I set it to some longer text, some of the text is cut off. 

    I must not change the size of the control. Actually I need  it for printing some cards, and the size of the cards is fixed.

    What I need: decrease the font size to make the text fit.

    How can I do this?

    Regards,
    Norbert Ruessmann
    • Edited by nruessmann Monday, October 27, 2008 11:46 AM
    Monday, October 27, 2008 11:45 AM

Answers

  • The idea is to use Viewbox in the label template. An example is shown below

    <Label Width="200" Height="300" Content="This is a very long text that you would be using to set the control text"
                <Label.Template> 
                    <ControlTemplate> 
                        <Viewbox> 
                            <ContentPresenter 
                  Content="{TemplateBinding Property=ContentControl.Content}" /> 
                        </Viewbox> 
                    </ControlTemplate> 
                </Label.Template> 
            </Label> 

    Works like magic with absolutely no coding required.

    Let me know if this helps.
    Monday, October 27, 2008 1:55 PM

All replies

  • As far as I know there's no easy way of doing this. You'll probably have to write some code to figure out the correct fontsize. To measure a text you can use the System.Windows.Media.FormattedText class.

    hth,
    Marcel
    Monday, October 27, 2008 12:27 PM
  • The idea is to use Viewbox in the label template. An example is shown below

    <Label Width="200" Height="300" Content="This is a very long text that you would be using to set the control text"
                <Label.Template> 
                    <ControlTemplate> 
                        <Viewbox> 
                            <ContentPresenter 
                  Content="{TemplateBinding Property=ContentControl.Content}" /> 
                        </Viewbox> 
                    </ControlTemplate> 
                </Label.Template> 
            </Label> 

    Works like magic with absolutely no coding required.

    Let me know if this helps.
    Monday, October 27, 2008 1:55 PM
  • Yep, the ViewBox does the trick. Totally forgot about that one :-)

    You can keep it easier by just wrapping the label in it, like this:

    <Viewbox> 
        <Label Content="This is a very long text that you would be using to set the control text"/>    
    </Viewbox> 
     
     

    Marcel
    Monday, October 27, 2008 2:25 PM
  • DutchMarcel said:

    Yep, the ViewBox does the trick. Totally forgot about that one :-)

    You can keep it easier by just wrapping the label in it, like this:

    <Viewbox> 
        <Label Content="This is a very long text that you would be using to set the control text"/>    
    </Viewbox> 
     
     

    Marcel


    Yes! that is what I initially tried but the Visual Tree for the label created that way is very deep compared to that when template is used.

    If template is used:
    Label->ViewBox->ContainerVisual->ContentPresenter->Textblock

    where as if it is wrapped up inside content. then the visual tree is
    Label->Border->ContentPresenter->Viewbox->ContainerVisual->Label->Border->ContentPresenter->Textblock

    Not sure if it effects performance, but i thought it would be nice.Moreover template could easily be applied to many labels.
    Monday, October 27, 2008 2:48 PM