我正在尝试在 WPF 中的 TextBlock 上设置FontStretch属性,但它似乎不起作用。我尝试了 Expanded、Condensed 等,但文本外观没有改变。
我正在使用 Framework 4.0 开发 Windows XP,并使用 Verdana 和 Arial 进行了测试。
它仅适用于 Windows 7 还是仅适用于某些特定字体?
编辑:如果它不适用于所有字体,是否有支持此功能的字体列表?或者是否可以修改像 Verdana/Arial 这样的字体来支持它?
要在不支持的字体中获得与 FontStretch 类似的效果,可以在 TextBlock 上使用 LayoutTransform:
<Application.Resources>
<ScaleTransform x:Key="FontStretchCondensed" ScaleX="0.8" />
<ScaleTransform x:Key="FontStretchExpanded" ScaleX="1.2" />
</Application.Resources>
...
<TextBlock Text="This is my text"
LayoutTransform="{StaticResource FontStretchCondensed}" />
如果您想让 TextBlocks 中的所有文本都显示为压缩,这也可以设置为样式:
<Style TargetType="TextBlock">
<Style.Setters>
<Setter Property="LayoutTransform" Value="{StaticResource FontStretchCondensed}" />
</Style.Setters>
</Style>
与粗体或斜体等字体属性不同,WPF 不模拟拉伸或小型大写字母等字体属性。使用的字体本身必须支持这一点。
要查看哪些字体支持 FontStretch,您需要查找具有单独字体文件的字体,例如 Condensed 或 Expanded。然后只有正确链接的 TrueType/OpenType 字体才能工作。一些字体供应商没有正确地将其字体的拉伸或压缩变体链接到常规版本,因此 WPF 不知道这些字体变体是相关的。
通过测量一些文本(TextBlock.Measure),我发现 Arial Narrow 是 Arial 宽度的 × 0.82。
So textBlock.LayoutTransform = new System.Windows.Media.ScaleTransform(0.82, 1.0);
when applied to Arial might approximate Arial Narrow.
我相信 FontStretch 并不适用于所有字体。