wpf 将 FontWeight 设置为 Bold 会缩小文本大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13088971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting FontWeight to Bold shrinks text size
提问by GrandMasterFlush
I've been setting up a resource dictionary to style all the controls in my WPF application and I've discovered some odd behaviour when setting the font weight for a label.
我一直在设置一个资源字典来为我的 WPF 应用程序中的所有控件设置样式,并且在设置标签的字体粗细时我发现了一些奇怪的行为。
I have to styles set up for labels, the first with normal font weight :
我必须为标签设置样式,第一个使用正常字体粗细:
<Style x:Key="Label" TargetType="{x:Type Label}">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="10,0"/>
</Style>
and the second set to bold :
第二个设置为粗体:
<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>
The trouble is that when I use the bold weighted font, the text shrinks (or the text spacing) :
麻烦的是,当我使用粗体加权字体时,文本会缩小(或文本间距):


I've searched about but I can't seem to see any reason for this, if anything I'd expect the text to expand because of the letter thickness increasing. Is this meant to happen and if so is there a way around it?
我已经搜索过,但似乎找不到任何原因,如果有的话,我希望文本会因为字母粗细增加而扩展。这是注定要发生的吗?如果是这样,有没有办法解决它?
EDIT :The window is using the following fonts:
编辑:该窗口使用以下字体:
<Setter Property="TextOptions.TextFormattingMode" Value="Display"/>
<Setter Property="FontFamily" Value="Calibri"/>
<Setter Property="FontSize" Value="12"/>
采纳答案by GrandMasterFlush
After a bit of investigation following on from Mark Hall and Sayed Saad's comments I managed to work out what was causing this: TextOptions.TextFormattingMode = Display.
在根据 Mark Hall 和 Sayed Saad 的评论进行一些调查后,我设法找出了导致这种情况的原因:TextOptions.TextFormattingMode = Display。
As Mark pointed out, the bold font text does get bigger than the normal font text when you up the font size. However, if I changed the TextFormattingMode to "Ideal" then the bold font is bigger than the normal font irrespective of the font size.
正如马克指出的那样,当您增大字体大小时,粗体文本确实会比普通字体文本大。但是,如果我将 TextFormattingMode 更改为“理想”,那么无论字体大小如何,粗体都比普通字体大。
EDIT:Based on my findings here I posted another question, the answer to this can be found here: TextOptions.TextFormattingMode affecting text with bold font weight
编辑:根据我的发现在这里我发布了另一个问题,可以在这里找到答案:TextOptions.TextFormattingMode Impact text with bold font weight
回答by Mark Hall
I am not sure what is going on, but for my guess something is overriding the FontSizeselection for the Bold Label. I can get about the same spacing as your example if the FontSizeis set to 11 instead of 12. I get this Image with the top 2 labels set for FontSize12 and the bottom label is set for a FontSizeof 11:
我不确定发生了什么,但据我推测,有些东西覆盖FontSize了粗体标签的选择。如果FontSize设置为 11 而不是 12,我可以获得与您的示例大致相同的间距。我得到这个图像,顶部 2 个标签设置为FontSize12,底部标签设置为FontSize11:


using this:
使用这个:
App.Xaml
应用程序.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
主窗口.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>
回答by sayed saad
It looks like you are not using the same font size. I tried two labels with same font size and margin. Actually the bold label expands.
看起来您没有使用相同的字体大小。我尝试了两个具有相同字体大小和边距的标签。实际上,粗体标签会扩大。

