Why FontStretch does not work in WPF?

Drake picture Drake · Jun 1, 2010 · Viewed 16.9k times · Source

I am trying setting FontStretch property on a TextBlock in WPF but it seems that it does not work. I tried Expanded, Condensed, etc. but the text appearance does not change.

I am working on Windows XP with Framework 4.0 and tested both with Verdana and Arial.

Does it work only on Windows 7 or only with some specific fonts?

EDIT: If it does not work with all fonts, is there a list of fonts that support this feature? Or is it possible to modify a font like Verdana/Arial to support it?

Answer

Ray Burns picture Ray Burns · Jun 5, 2010

To get a similar effect to FontStretch in a font that doesn't support it, you can use a LayoutTransform on the TextBlock:

<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}" />

This can also be set in a style if you want to have all text in TextBlocks appear condensed:

<Style TargetType="TextBlock">
  <Style.Setters>
    <Setter Property="LayoutTransform" Value="{StaticResource FontStretchCondensed}" />
  </Style.Setters>
</Style>