wpf 如何缩小 Textblock 中的字体大小以适应内容的宽度并保持字体纵横比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18638968/
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
How to shrink font size in Textblock to fit width of content AND maintain font aspect ratio
提问by user2373715
Here's the deal: I have this line of XAML
这是交易:我有这行 XAML
<Viewbox Canvas.Left="27" Canvas.Top="479" Width="377" Height="21" Stretch="Fill"
StretchDirection="DownOnly" VerticalAlignment="Top" HorizontalAlignment="Left">
<TextBlock Name="TitleText" TextWrapping="Wrap" TextAlignment="Left" FontSize="14"
FontStretch="Normal" FontStyle="Normal" Foreground="White" Width="377" >some long text here
</TextBlock>
</Viewbox>
I want the font to scale down to fit the contents height and width. What happens now is that the font scales down, but the Viewbox also scales the content horizontally, making the width of the textbox smaller. Here's an example
我希望字体缩小以适应内容的高度和宽度。现在发生的情况是字体缩小了,但 Viewbox 也会水平缩放内容,从而使文本框的宽度变小。这是一个例子
If I set Stretch="Fill" the text will fill the widht, but shrinks only font-size in heigth, making the font look horribly ugly Like this.
如果我设置 Stretch="Fill" 文本将填充宽度,但只缩小字体大小的高度,使字体看起来非常难看像这样。
Is it possible to shrink the text to fit inside a frame so that it uses the whole width and height of the container?
是否可以缩小文本以适应框架,以便使用容器的整个宽度和高度?
回答by Jan Rothkegel
You may not set the StretchDirection property of your viewbox to "DownOnly". This setting leads to the effect that the content only gets stretched vertical.
您不能将视图框的 StretchDirection 属性设置为“DownOnly”。此设置导致内容仅垂直拉伸的效果。
回答by Sheridan
Is this what you want?
这是你想要的吗?
<Viewbox Stretch="Uniform">
<TextBlock Name="TitleText" TextWrapping="Wrap" TextAlignment="Left" FontSize="14"
FontStretch="Normal" FontStyle="Normal" Foreground="White" Width="377">
some long text here
</TextBlock>
</Viewbox>
回答by Marcus
Here there is a possible solution using a second ViewBox:
这里有一个使用第二个 ViewBox 的可能解决方案:
<Viewbox Canvas.Left="27" Canvas.Top="479" Width="377" Height="21" Stretch="Fill"
StretchDirection="DownOnly" VerticalAlignment="Top" HorizontalAlignment="Left">
<Viewbox Stretch="Fill" Width="Auto" Height="Auto">
<TextBlock Name="TitleText" TextWrapping="Wrap" TextAlignment="Left" FontSize="14"
FontStretch="Normal" FontStyle="Normal" Foreground="White" Width="377" >some long text here
</TextBlock>
</Viewbox>
</Viewbox>

