2

I want to learn how to utilize fontstretch in my wpf applications.

I've created this simple usercontrol, a border with rounded corners which has a textblock. I want to stretch the text of the textblock to fill my border. I want to avoid the use of the viewbox control to do this.

this is my usercontrol xaml

 <UserControl x:Class="DisplayObject"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="400" Background="Transparent">
    <UserControl.Resources>
        <LinearGradientBrush x:Key="BackGroundBrush" StartPoint="0,0" EndPoint="1,1">
            <GradientStop Color="AntiqueWhite" Offset="0"/>
            <GradientStop Color="White" Offset="0.45" />
            <GradientStop Color="Silver" Offset="1" />
        </LinearGradientBrush>
    </UserControl.Resources>
    <Border x:Name="LayoutRoot" CornerRadius="12" Background="{StaticResource BackGroundBrush}" BorderBrush="Black" BorderThickness="2">
        <TextBlock TextAlignment="Center" Text="{Binding Path=DisplayText}" 
                   Background="Transparent" HorizontalAlignment="Center" VerticalAlignment="Center" 
                   TextWrapping="Wrap" FontSize="12" FontFamily="Arial" FontStretch="UltraExpanded"/>
    </Border>
</UserControl>

From what I gather from reading online the Arial font is an opentype so it supports stretching. I tried using horizontal/vertical alignment values of "Stretch" but this did not help. Not sure what I have done wrong but I figured someone on this site may be able to explain why its not stretching for me, and how to fix it.

Thanks for reading my post.

TWood
  • 2,563
  • 8
  • 36
  • 58

1 Answers1

4

The Arial font does not seem to support the FontStretch value of UltraExpanded. Try the value of UltraCondensed instead to see it work:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" Text="{Binding DisplayText}" FontSize="30" 
        FontFamily="Arial" HorizontalAlignment="Center" VerticalAlignment="Center" />
    <TextBlock Grid.Row="1" Text="{Binding DisplayText}" FontSize="30" 
        FontFamily="Arial" HorizontalAlignment="Center" VerticalAlignment="Center" 
        FontStretch="UltraCondensed" />
</Grid>

Look at the Why FontStretch does not work in WPF? post to find out an alternative to using this little used property.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • UltraCondensed does seem to squish everything together but it looks like Arial don't really support much other than that particular value. I did see that thread you referenced, it's where I learned that OpenType fonts must be used. I will try scaletransform and see if it can fit my needs. I'm not sure I understand why the framework kept the fontstretch property if scaletransform ends up being what you use to do something like this? – TWood Aug 07 '13 at 14:15
  • WPF has no control over which fonts support certain features... I think it's really more of a question why the font creators aren't providing all of the 'variation options'. – Sheridan Aug 07 '13 at 14:36
  • It looks like scaletransform is going to be what I have to use. The support is just better with that method instead of fontstretch. Now what I need to work on is writing a converter to calculate the scale value given various sizes of my usercontrol. Sort of like what viewbox can do but hopefully with less overhead. – TWood Aug 14 '13 at 00:32