Page 1 of 1

How to set/change font family and size globally in Unity?

Posted: 24 Jun 2019, 08:05
by horeaper
Back in WPF I can achieve this by setting Window's OverrideMetadata() in App class's constructor.
Now in Unity, App is no where to be found. My last (experimental) project went to set FontFamily/FontSize style with almost every control in a resources dictionary, and merge it on every xaml file. This (production) project, I kinda want to avoid that.

Also I want to change font whenever user changes the display language. I have no idea how to do that...

Re: How to set/change font family and size globally in Unity?

Posted: 25 Jun 2019, 10:52
by sfernandez
As FontFamily and FontSize are properties that inherit down the tree you can set them in the root of the main xaml. If you don't explicitly set those properties in control styles, then the inherited value will apply and override the entire UI.

To change the value depending on the language you can use a DynamicResource and then insert the corresponding dictionary in root Resources:
<UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  FontFamily="{DynamicResource DefaultFont}"
  FontSize="{DynamicResource DefaultFontSize}">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Langs/EnglishResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    ...
</UserControl>
Is this something that could work for you?

Re: How to set/change font family and size globally in Unity?

Posted: 25 Jun 2019, 11:19
by horeaper
As FontFamily and FontSize are properties that inherit down the tree you can set them in the root of the main xaml. If you don't explicitly set those properties in control styles, then the inherited value will apply and override the entire UI.

To change the value depending on the language you can use a DynamicResource and then insert the corresponding dictionary in root Resources:
<UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  FontFamily="{DynamicResource DefaultFont}"
  FontSize="{DynamicResource DefaultFontSize}">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Langs/EnglishResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    ...
</UserControl>
Is this something that could work for you?
I forgot DynamicResource... How stupid I am...🤣