5

I have a TextBlock that has a fixed size and variable text. I want my text to always fit in the TextBlock (without ellipsis or overflow, but with word wrap). If the text changes, the font size should be adjusted so that the text fits to the bounds of the TextBlock.

I know I could pack the TextBlock into a Viewbox (which I'm doing currently) but than I don't have word wrap.

The image above shows three different TextBlocks (the blue numbers are just for illustration). If I use a Viewbox I get results like in 1 and 2 but I rather want results like in 1 and 3.

So I'm basically trying to combine Viewbox and word wrap. How can this be done?

Timo
  • 9,269
  • 2
  • 28
  • 58
  • You can use the [TextWrapping](https://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.textwrapping(v=vs.110).aspx) property to get the wrapping behavior. – pstrjds Dec 23 '17 at 22:29
  • @pstrjds Unfortunately not. If my TextBlock is in a Viewbox (to achive different font sizes) the Textblock has (theoretical) infinite space and thus wrapping doesn't kick in because the text fits in a single line. – Timo Dec 23 '17 at 22:32
  • My misunderstanding. At first read of your question I thought you wanted to get rid of the `Viewbox`, on a re-read, I see you want to keep it. – pstrjds Dec 23 '17 at 22:33
  • @pstrjds Not neccessarly. If I get both features (wrapping and maximum font size), I don't need the Viewbox. I just use it to provide the adjustable font size. – Timo Dec 23 '17 at 22:35
  • I think you could get rid of the `Viewbox` if you combined the wrapping and the answer from this question to detect when the text is being trimmed, and then adjust the font until it is no longer being trimmed - https://stackoverflow.com/a/25436070/416574 – pstrjds Dec 23 '17 at 22:42

1 Answers1

1

If you restrict the viewbox expansion to one dimension, then you can still have your wrap and something close to #3. Try this answer

reti
  • 67
  • 2