【问题标题】:Setting FontWeight to Bold shrinks text size将 FontWeight 设置为 Bold 会缩小文本大小
【发布时间】:2012-10-16 20:09:20
【问题描述】:

我一直在设置资源字典来设置 WPF 应用程序中所有控件的样式,并且在为标签设置字体粗细时发现了一些奇怪的行为。

我必须为标签设置样式,第一个具有正常字体粗细:

<Style x:Key="Label" TargetType="{x:Type Label}">
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="Margin" Value="10,0"/>
</Style>

第二个设置为粗体:

<Style x:Key="LabelBold" TargetType="{x:Type Label}">
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="Margin" Value="10,0"/>
    <Setter Property="FontWeight" Value="Bold"/>
</Style>

问题是当我使用粗体加权字体时,文本会缩小(或文本间距):

我已经搜索过,但似乎找不到任何原因,如果有的话,我希望文本会因为字母厚度增加而扩大。 这是注定要发生的吗?如果是这样,有没有办法解决它?

编辑:窗口使用以下字体:

<Setter Property="TextOptions.TextFormattingMode" Value="Display"/>
<Setter Property="FontFamily" Value="Calibri"/>
<Setter Property="FontSize" Value="12"/>

【问题讨论】:

  • 只是出于好奇,您使用的是什么 FontFamilyFontName
  • @MarkHall 添加了详细信息。

标签: wpf xaml


【解决方案1】:

我不确定发生了什么,但我猜有些东西覆盖了粗体标签的 FontSize 选择。如果 FontSize 设置为 11 而不是 12,我可以获得与您的示例大致相同的间距。我得到这张图片,前 2 个标签设置为 FontSize 12,底部标签设置为 FontSize 11:

使用这个:

App.Xaml

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style x:Key="Label" TargetType="{x:Type Label}">
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="10,0"/>
        </Style>
        <Style x:Key="LabelBold" TargetType="{x:Type Label}">
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="10,0"/>
            <Setter Property="FontWeight" Value="Bold"/>
        </Style>
        <Style x:Key="WindowStyle" TargetType="{x:Type Window}">
            <Setter Property="TextOptions.TextFormattingMode" Value="Display"/>
            <Setter Property="FontFamily" Value="Calibri"/>
            <Setter Property="FontSize" Value="12"/>
        </Style>
    </Application.Resources>
</Application>

MainWindow.xaml

Window x:Class="WpfApplication1.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Style="{StaticResource WindowStyle}">
    <Grid>
        <Label Style="{StaticResource Label}" Height="32" HorizontalAlignment="Left" Margin="10,10,0,0" Name="label1" VerticalAlignment="Top">This is a test of font-weight:</Label>
        <Label Style="{StaticResource LabelBold}" Height="32" HorizontalAlignment="Left" Margin="10,30,0,0" Name="label2" VerticalAlignment="Top">This is a test of font-weight:</Label>
        <Label Style="{StaticResource LabelBold}" Height="32" HorizontalAlignment="Left" Margin="10,50,0,0" FontSize="11" Name="label5" VerticalAlignment="Top">This is a test of font-weight:</Label>
    </Grid>
</Window>

【讨论】:

  • 感谢您的建议,马克,根据您所说的,我对样式有所了解,并且能够找出确切的问题。
【解决方案2】:

在 Mark Hall 和 Sayed Saad 的 cmets 进行了一些调查后,我设法找出了导致此问题的原因:TextOptions.TextFormattingMode = Display。

正如 Mark 指出的那样,当您增大字体大小时,粗体文本确实会比正常字体文本更大。但是,如果我将 TextFormattingMode 更改为“理想”,那么无论字体大小如何,粗体字体都会比普通字体大。

编辑:根据我在这里的发现,我发布了另一个问题,可以在这里找到答案:TextOptions.TextFormattingMode affecting text with bold font weight

【讨论】:

    【解决方案3】:

    您似乎没有使用相同的字体大小。我尝试了两个具有相同字体大小和边距的标签。实际上粗体标签扩大了。

    【讨论】:

    • 感谢您查看 Sayed,您是否设置了 TextOptions.TextFormattingMode 的值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多