问题描述
对于一个固定大小的可打包文本区域,是否有任何方法可以根据文本的数量尽可能地增大字体大小?例如,if你有一个500x500的文字你好的地区,字体大小将是非常大的。但如果你有一段文字的字体大小将会更小,以适应该地区。
我已经看过视框但看不到它可以工作
任何可以做到这一点的xaml或代码都可以帮助(不一定是特定的控件)。
<$>
p $ p> < DockPanel x:Name =LayoutRoot>
< TextBox x:Name =textText =这是一些文字和一些更多的文字,我没有看到任何问题...DockPanel.Dock =TopTextChanged =text_TextChanged/> ;
< TextBlock DockPanel.Dock =TopText ={Binding ElementName = tb,Path = FontSize}/>
< Border Name =bdBorderBrush =BlackBorderThickness =1>
< TextBlock Name =tbText ={Binding ElementName = text,Path = Text}TextWrapping =Wrap/>
< / Border>
< / DockPanel>
而在代码后面:
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();
试试吧,拖动它,改变文字...
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).
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...
这篇关于WPF / XAML - 文本可以自动调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!