【问题标题】:working with fontstretch in wpf在 wpf 中使用 fontstretch
【发布时间】:2013-08-08 20:41:42
【问题描述】:

我想学习如何在我的 wpf 应用程序中使用 fontstretch。

我创建了这个简单的用户控件,一个带有文本块的圆角边框。我想拉伸文本块的文本以填充我的边框。我想避免使用 viewbox 控件来执行此操作。

这是我的用户控件 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>

根据我从网上阅读得到的信息,Arial 字体是一种开放式字体,因此它支持拉伸。我尝试使用“拉伸”的水平/垂直对齐值,但这没有帮助。不知道我做错了什么,但我认为这个网站上的某个人可能能够解释为什么它对我来说没有拉伸,以及如何解决它。

感谢您阅读我的帖子。

【问题讨论】:

    标签: wpf font-size


    【解决方案1】:

    Arial 字体似乎不支持UltraExpandedFontStretch 值。尝试使用 UltraCondensed 的值来查看它是否有效:

    <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>
    

    查看Why FontStretch does not work in WPF? 帖子,找出使用这个很少使用的属性的替代方法。

    【讨论】:

    • UltraCondensed 似乎确实将所有内容压缩在一起,但看起来 Arial 除了该特定值之外并没有真正支持太多。我确实看到了您引用的那个线程,这是我了解到必须使用 OpenType 字体的地方。我会尝试 scaletransform 看看它是否能满足我的需求。如果 scaletransform 最终成为您用来执行此类操作的工具,我不确定我是否理解为什么框架会保留 fontstretch 属性?
    • WPF 无法控制哪些字体支持某些功能...我认为字体创建者为什么不提供所有“变体选项”实际上更多的是一个问题。
    • 看起来 scaletransform 将成为我必须使用的。使用该方法而不是 fontstretch 的支持更好。现在我需要做的是编写一个转换器来计算给定不同大小的用户控件的比例值。有点像 viewbox 可以做的,但希望开销更少。
    猜你喜欢
    • 1970-01-01
    • 2011-08-17
    • 2011-08-12
    • 2016-07-14
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多