Setting FontWeight to Bold Decreases Text Size

I set up a resource dictionary to style all the controls in my WPF application, and I found some odd behavior when setting the font weight for the label.

I have styles configured for labels, the first with the usual 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 is 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 problem is that when I use bold, the text is compressed (or text spacing):

enter image description here

I searched, but apparently I see no reason for this, if I expect something that the text will expand due to the increase in the thickness of the letter. Does this mean that this will happen, and if so, that is around?

EDIT: The following fonts are used in the window:

 <Setter Property="TextOptions.TextFormattingMode" Value="Display"/> <Setter Property="FontFamily" Value="Calibri"/> <Setter Property="FontSize" Value="12"/> 
+2
source share
3 answers

After a little investigation following the comments of Mark Hall and Syed Saad, I was able to figure out what causes this: TextOptions.TextFormattingMode = Display.

As Mark noted, a bold font becomes larger than regular font text as the font size increases. However, if I changed TextFormattingMode to "Ideal", the bold font will be larger than the regular font, regardless of the font size.

EDIT:. Based on my findings, I added another question: the answer to this question can be found here: TextOptions.TextFormattingMode, which affects bold text

+3
source

It looks like you are not using the same font size. I tried two labels with the same size and difference. In fact, the bold label expands.

+2
source

I'm not sure what is going on, but for my guess, something is overriding the FontSize selection for the bold label. I can get about the same distance as your example if FontSize set to 11 instead of 12. I get this image with the top two labels set for FontSize 12 and the bottom label set for FontSize 11:

enter image description here

using this:

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
source

Source: https://habr.com/ru/post/1442994/


All Articles