AvaloniaCommunity/Material.Avalonia

Proper FontFamily setting

Closed this issue · 4 comments

Discussed in #406

Originally posted by PRNDA October 7, 2024
Hey everyone,
I want to replace the default font in Material Avalonia globally. I have googled around for a while, but nothing useful has been found.

The default font is defined as a FontFamily resource name MaterialDesignFonts in Material.Styles/Resources/Themes/FontFamily.axaml.

I tried creating resources with the same name in Application.Resources, but this didn't work.

<Application>
  <Application.Styles>
    <themes:MaterialTheme BaseTheme="Light" PrimaryColor="Green" SecondaryColor="Blue" />
  </Application.Styles>

  <Application.Resources>
    <FontFamily x:Key="MaterialDesignFonts">
      Microsoft Yahei, 微软雅黑,Simsun, SimHei, PingFang SC, 苹方-简, 宋体-简, 宋体, Microsoft JhengHei, Microsoft JhengHei UI, $Default
    </FontFamily>

  </Application.Resources>
</Application>

Screenshot:
image

Finally, I ended up defining several styles applied to TextBlock/TextBox/AccessText, like this:

<Application >
  <Application.Resources>
    <FontFamily x:Key="MaterialDesignFonts">
      Microsoft Yahei, 微软雅黑,Simsun, SimHei, PingFang SC, 苹方-简, 宋体-简, 宋体, Microsoft JhengHei, Microsoft JhengHei UI, $Default
    </FontFamily>
  </Application.Resources>

  <Application.Styles>
    <themes:MaterialTheme BaseTheme="Light" PrimaryColor="Green" SecondaryColor="Blue" />

    <Style Selector="TextBlock">
      <Setter Property="FontFamily" Value="{StaticResource MaterialDesignFonts}" />
    </Style>
    <Style Selector="TextBox">
      <Setter Property="FontFamily" Value="{StaticResource MaterialDesignFonts}" />
    </Style>
    <Style Selector="AccessText">
      <Setter Property="FontFamily" Value="{StaticResource MaterialDesignFonts}" />
    </Style>

  </Application.Styles>

</Application>

image

How about Button font? it can't setting font like your example.

I tried this, but did't work.

		<Style Selector="Button.Content">
			<Setter Property="FontFamily">
				Microsoft YaHei
			</Setter>
		</Style>
	the only way I can fix this, is set the button's content as a TextBlock.
	
	like:
				<Button
					Grid.Column="0"
					Height="35"
					HorizontalAlignment="Left"
					Classes="light"
					Command="{Binding AddCroppingConfigCommand}"
					CommandParameter="{Binding ElementName=DragCanvas}">
					<TextBlock Text="ABCDEFG"/>
				</Button>

Yes, you need to target AccessText inside of a Button with a selector somewhat like this: Button.Content AccessText. This will change when this issue will be fixed.