I'm looking for method to change font size in TextBlock if text goes to second line. 
How can I know, when the text proceed to the second line? 
Thanks for help.
            Asked
            
        
        
            Active
            
        
            Viewed 262 times
        
    1
            
            
         
    
    
        SlaviS
        
- 21
- 1
- 5
- 
                    Are you trying to keep it from wrapping? – Mike Perrenoud Feb 21 '14 at 13:32
- 
                    Yes, but I don't want to have horizontal scroll... I'd like to decrease the text – SlaviS Feb 21 '14 at 13:33
- 
                    What will happen when the text is extremely long? – Jonesopolis Feb 21 '14 at 13:35
- 
                    @Jonesy: very good point. Further, @SlaviS, you'll probably need to consume the [`SizeChanged`](http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.sizechanged(v=vs.110).aspx) event and then change the `FontSize` appropriately to get it to size back to its original size. – Mike Perrenoud Feb 21 '14 at 13:37
- 
                    @Jones Nothing, I trying to make a calculator. So, my text will have max 20 character, but for start the font size is really big, and I'd like to decrease it when the text won't to fit in the TextBlock. – SlaviS Feb 21 '14 at 13:39
2 Answers
0
            
            
        What you could do is handle the TextChanged event and in the handler check the length of the text.
If the text is <= 20 you keep the original size. If it's > 20 but <= 25 you decrease the font size by say 3 points. An so on until you reach a limit of characters you want to support, say 50 or something.
You will probably need to disable Textbox text wrapping so that you only have a single line of text.
 
    
    
        Alex
        
- 1,110
- 8
- 15
- 
                    :) Well you are changing text from somewhere right? Just do this there then. – Alex Feb 22 '14 at 20:10
0
            
            
        Wrapping with a Viewbox may give you the functionality you're after:
<Viewbox Stretch="Uniform" Width="50" Height="50">
    <TextBlock Text="Test" />
</Viewbox>
- 
                    You'll need to set the Width and Height of the Viewbox to be the size of you want for your calculator display rather than the height and width of the TextBlock. I tested the above in kaxaml and it worked okay. Can you provide an example of your non-working code? – thudbutt Feb 22 '14 at 17:12
- 
                    So, for example. I've TextBlock 200x400 and I'd like to display text in field 200x300. I have to add Viewbox with this width and height? – SlaviS Feb 26 '14 at 21:13
- 
                    If you have a 200x400 TextBlock and you want to display text in a field that is 200x300 within that and (according to your original question) want the text to autosize then yes. You will need a TextBlock of 200x400 containing a ViewBox of 200x300 which contains a text block with your text. Basically a 200x400 TextBlock containing the above answer but with and height set to 200 and 300 respectively. – thudbutt Feb 26 '14 at 23:25
 
    