Reputation: 10968
For a fixed size wrappable text area, is there any way to make the font size as large as possible based on the amount of text?
For example, if you have a 500x500 area with the text "Hello", the font size would be really big. But if you have a paragraph of text the font size would be smaller to fit into the area.
I have looked at Viewbox but can't see that it could work with wrappable text.
ANY xaml or code that could do this would help (doesn't have to be a specific control).
Upvotes: 4
Views: 2021
Reputation: 32629
What you're asking is more complex than it sounds, but I'll give you an idea:
<DockPanel x:Name="LayoutRoot">
<TextBox x:Name="text" Text="this is some text and some more text I don't see any problems..." DockPanel.Dock="Top" TextChanged="text_TextChanged"/>
<TextBlock DockPanel.Dock="Top" Text="{Binding ElementName=tb, Path=FontSize}"/>
<Border Name="bd" BorderBrush="Black" BorderThickness="1">
<TextBlock Name="tb" Text="{Binding ElementName=text, Path=Text}" TextWrapping="Wrap"/>
</Border>
</DockPanel>
And in code behind:
public MainWindow()
{
this.InitializeComponent();
RecalcFontSize();
tb.SizeChanged += new SizeChangedEventHandler(tb_SizeChanged);
}
void tb_SizeChanged(object sender, SizeChangedEventArgs e)
{
RecalcFontSize();
}
private void RecalcFontSize()
{
if (tb == null) return;
Size constraint = new Size(tb.ActualWidth, tb.ActualHeight);
tb.Measure(constraint);
while (tb.DesiredSize.Height < tb.ActualHeight)
{
tb.FontSize += 1;
tb.Measure(constraint);
}
tb.FontSize -= 1;
}
private void text_TextChanged(object sender, TextChangedEventArgs e)
{
RecalcFontSize();
}
Try it, drag it around, change the text...
Upvotes: 3