将FontWeight设置为Bold会缩小文本大小

时间:2012-10-26 14:35:55

标签: wpf xaml

我一直在设置一个资源字典来设置我的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>

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

enter image description here

我已经搜索了但是我似乎没有看到任何理由,如果有什么我期望文字扩大,因为字母厚度增加。 这是否意味着要发生,如果有的话有办法吗?

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

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

3 个答案:

答案 0 :(得分:4)

我不确定发生了什么,但是我猜是有些事情会覆盖Bold Label的FontSize选项。如果FontSize设置为11而不是12,我可以获得与示例相同的间距。我得到此图像,其中前2个标签设置为FontSize 12,底部标签设置为FontSize的11:

enter image description here

使用:

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

答案 1 :(得分:3)

经过马克霍尔和赛义萨德的评论之后的一些调查,我设法解决了造成这种情况的原因:TextOptions.TextFormattingMode =显示。

正如Mark指出的那样,当您提高字体大小时,粗体字体文本确实比普通字体文本大。但是,如果我将TextFormattingMode更改为“Ideal”,则粗体字体大于普通字体,与字体大小无关。

编辑:根据我的调查结果,我发布了另一个问题,可以在此处找到答案:TextOptions.TextFormattingMode affecting text with bold font weight

答案 2 :(得分:2)

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