5

I have multiple .ttf file from the same family, like:

MyFont.ttf
MyFont_Bold.ttf
MyFont_Light.ttf
MyFont_Medium.ttf

If I want to use each weight, I should define theme as separate FontFamily like this:

<FontFamily x:Key="MyFont">
    ms-appx:///Fonts/MyFont.ttf#MyFont
</FontFamily>
<FontFamily x:Key="MyFont_Bold">
    ms-appx:///Fonts/MyFont_Bold.ttf#MyFont
</FontFamily>
<FontFamily x:Key="MyFont_Light">
    ms-appx:///Fonts/MyFont_Light.ttf#MyFont
</FontFamily>
<FontFamily x:Key="MyFont_Medium">
    ms-appx:///Fonts/MyFont_Medium.ttf#MyFont
</FontFamily>

And use them like:

<TextBlock FontFamily="{StaticResource MyFont}" />
<TextBlock FontFamily="{StaticResource MyFont_Bold}" />
<TextBlock FontFamily="{StaticResource MyFont_Light}" />
<TextBlock FontFamily="{StaticResource MyFont_Medium}" />

Is there any way to use theme like this:

<TextBlock FontFamily="{StaticResource MyFont}" />
<TextBlock FontFamily="{StaticResource MyFont}" FontWeight="Bold" />
<TextBlock FontFamily="{StaticResource MyFont}" FontWeight="Light" />
<TextBlock FontFamily="{StaticResource MyFont}" FontWeight="Medium" />
double-beep
  • 5,031
  • 17
  • 33
  • 41
TheSETJ
  • 518
  • 9
  • 25

1 Answers1

0

TextBlock has a TextBlock.FontWeight property. With this property, we can set font weight for TextBlock. So you can use some code like following:

<TextBlock FontFamily="{StaticResource MyFont}" FontWeight="Bold">The quick brown fox jumps over the lazy dog.1234567890</TextBlock>
<TextBlock FontFamily="{StaticResource MyFont}" FontWeight="Light">The quick brown fox jumps over the lazy dog.1234567890</TextBlock>
<TextBlock FontFamily="{StaticResource MyFont}" FontWeight="Medium">The quick brown fox jumps over the lazy dog.1234567890</TextBlock>

However while using custom fonts, the thickness of the character outlines is calculated according to the font you've set. It can't using the corresponding font file according to the FontWeight. The calculated thickness might be different from what in your custom fonts. For example:

<TextBlock FontFamily="Assets/Fonts/consola.ttf#Consolas" FontSize="36">The quick brown fox jumps over the lazy dog.1234567890</TextBlock>
<TextBlock FontFamily="Assets/Fonts/consolab.ttf#Consolas" FontSize="36">The quick brown fox jumps over the lazy dog.1234567890</TextBlock>
<TextBlock FontFamily="Assets/Fonts/consola.ttf#Consolas" FontSize="36" FontWeight="Bold">The quick brown fox jumps over the lazy dog.1234567890</TextBlock>

In above sample, "consola.ttf" is the font file with regular font weight and "onsolab.ttf" is the font file with bold font weight. In the third TextBlock, I used the regular font file and set FontWeight to Bold. And the output looks like:
enter image description here

As you can see after setting FontWeight to Bold, the thickness of the character outlines is larger but its not the same as the one using the bold weight font file. So you may need to consider whether to use FontWeight or different font files according to your actual scenario.

Jay Zuo
  • 15,653
  • 2
  • 25
  • 49
  • 2
    Thank you for your answer, but this is exactly what I'mm asking. I need a way to match up FontWeight property with corresponding font file or whatever that make it possible to use FontWeight against separate font. – TheSETJ Nov 24 '16 at 15:53