[英]How to shrink font size in Textblock to fit width of content AND maintain font aspect ratio
這是交易:我有這行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>
我希望字體縮小以適合內容的高度和寬度。 現在發生的是字體縮小,但“視圖框”也水平縮放了內容,使文本框的寬度變小。 這是一個例子
如果我設置Stretch =“ Fill”,則文本將填充整個寬度,但僅縮小字體大小(以高為單位),從而使字體看起來像這樣難看。
是否可以將文本縮小以適合框架內,以便使用容器的整個寬度和高度?
您不能將視圖框的StretchDirection屬性設置為“ DownOnly”。 此設置導致以下效果:僅將內容垂直拉伸。
這是你想要的嗎?
<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>
這是使用第二個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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.