WPF TextBlock 字体调整大小以填充网格中的可用空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2282662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:30:30  来源:igfitidea点击:

WPF TextBlock font resize to fill available space in a Grid

wpffontsresizetextblock

提问by Nate Zaugg

I have some text that is displayed at run time in a textblock. I want the font size to be the biggest it can be to fill the area that is given. I think I have the textblock setup correctly to "autosize" and I try to increase the font size till the textblock is bigger than than its parent then decrease the font size by 1. The problem is I can't get the control to redraw/recompute its size.

我有一些在运行时显示在文本块中的文本。我希望字体大小是最大的,可以填充给定的区域。我想我已经正确地将文本块设置为“自动调整大小”,我尝试增加字体大小,直到文本块大于其父级,然后将字体大小减小 1。问题是我无法重新绘制/重新计算它的大小。

Is the a better way to do that? Or is there a way I can make my method work?

是更好的方法吗?或者有什么方法可以使我的方法起作用?

回答by Jobi Joy

Wrap the TextBlockinside a ViewBox:

TextBlock里面包起来ViewBox

   <Grid>
    <Viewbox>
        <TextBlock TextWrapping="Wrap" Text="Some Text" />
    </Viewbox>
   </Grid>

回答by alireza alimardan

I had the same problem. You can use this to resizethe fontsizeof the textblockto fill the area when it has overflow.

我有同样的问题。你可以用它来调整大小字体大小的的文本块,以填补该地区拥有时溢出



<Viewbox StretchDirection="DownOnly" Stretch="Uniform">
    <TextBlock Text="{Binding Path=Title}" HorizontalAlignment="Center"/>
</Viewbox>


回答by Sajin

The WPF ViewBoxcontrol can grow / shrink with its contents to the available space.

WPFViewBox控件可以随其内容增长/缩小到可用空间。

Just place your TextBlockwithin a ViewBoxas;

只需将您的TextBlock放在ViewBoxas 中;

<Viewbox Stretch="Uniform" Width="50" Height="50">
    <TextBlock Text="Test" />
</Viewbox>

ViewBoxis typically scaled by its container.

ViewBox通常由其容器缩放。

回答by Nate Zaugg

I found a great way to do this using ViewBox:

我找到了一个很好的方法来做到这一点ViewBox

 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="50" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="100" />
    </Grid.ColumnDefinitions>
    <Viewbox Grid.Row="0" Grid.Column="0" Stretch="Uniform">
        <TextBlock Name="tbTest" Background="Yellow" Text="This is some text" />    
    </Viewbox>

    <ContentControl Grid.Column="0" Grid.Row="2">
        <TextBlock>This is some text</TextBlock>
    </ContentControl>
</Grid>

回答by Nate Zaugg

Well, its not a "perfect" answer, but this is a quick hack (you can drop this into kaxaml and test it out):

好吧,这不是一个“完美”的答案,但这是一个快速的技巧(您可以将其放入 kaxaml 并进行测试):

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid Height="300" Background="green">  
  <Viewbox>
  <TextBlock Background="red" Text="Hurr"/>
  </Viewbox>
  </Grid>
</Page>

The ViewBoxwill enlarge any content to fill its container. The problem is that the TextBlock, while it sizes to its text, has padding at the top and bottom that you can't get rid of (without doing some heavy lifting). This might get you closer to what you want, tho.

ViewBox会放大任何内容来填充它的容器。问题是TextBlock,虽然它的大小适合它的文本,但在顶部和底部有填充,你无法摆脱(不做一些繁重的工作)。这可能会让你更接近你想要的,不过。

回答by reti

To ensure wrapping, you need to set the MaxWidthand/or MaxHeightof the TextBlock

为了确保包裹,你需要设置MaxWidth和/或MaxHeightTextBlock

<Viewbox StretchDirection="DownOnly" Stretch="Uniform">
      <TextBlock  MaxWidth="500" TextWrapping="Wrap" FontSize="30" VerticalAlignment="Center"
                  Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."/>