Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper FontFamily setting #407

Closed
SKProCH opened this issue Oct 8, 2024 Discussed in #406 · 4 comments · Fixed by #420
Closed

Proper FontFamily setting #407

SKProCH opened this issue Oct 8, 2024 Discussed in #406 · 4 comments · Fixed by #420
Assignees
Labels
bug Something isn't working

Comments

@SKProCH
Copy link
Collaborator

SKProCH commented Oct 8, 2024

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.

@SKProCH SKProCH added the bug Something isn't working label Oct 8, 2024
@SKProCH SKProCH self-assigned this Oct 8, 2024
@PRNDA
Copy link

PRNDA commented Oct 9, 2024

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

@mehaifeng
Copy link

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

@mehaifeng
Copy link

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>

@SKProCH
Copy link
Collaborator Author

SKProCH commented Dec 23, 2024

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.

SKProCH added a commit that referenced this issue Dec 25, 2024
Remove explicit FontFamily setters in styles, allowing to inherit font properly. Fix #407
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants