WPF Textblock zoom and wrap
Asked Answered
J

1

6

I have a textblock which needs to

  1. show text in the maximum font size possible within the given space (less characters bigger letters and vice-versa)
  2. wrap the text within the given space.

I tried using a textbox inside a viewbox like below but the text-wrapping doesn't work if i don't specify the textblock width and height. If i do specify the width and height to the same size as the viewbox, obviously zooming doesn't happen.

<Viewbox Stretch="Fill" Width="100" Height="100">
<TextBlock TextWrapping="Wrap"/>
</Viewbox>

Is there any other way to acheive this? Or should i think about writing an algorithm to increase font size manually based on the amount of text? Any help is appreciated.

Jdavie answered 26/9, 2010 at 3:16 Comment(1)
You could manually do the line break by handling the PreviewKeyDown or PreviewKeyUp event and check if the length limit of your text was reached. Another way to achieve the same result: Create a custom Converter and do the line break there. You could pass a parameter or use a Field to specify MaxLength.Forget
M
0

Try this code.

XAML:

<TextBlock x:Name="textBlock"
           Text="Something text"
           TextWrapping="Wrap"
           FontSize="1"
           Width="100"
           Opacity="0" />

Code behind:

while (textBlock.ActualHeight <= 100)
{
    textBlock.FontSize += 0.1;
    textBlock.UpdateLayout();
}
textBlock.FontSize -= 0.1;
textBlock.Opacity = 1;
Manhunt answered 14/11, 2014 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.