User avatar
horeaper
Topic Author
Posts: 34
Joined: 22 Sep 2014, 12:50

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

24 Jun 2019, 08:05

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...
 
User avatar
sfernandez
Site Admin
Posts: 3008
Joined: 22 Dec 2011, 19:20

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

25 Jun 2019, 10:52

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?
 
User avatar
horeaper
Topic Author
Posts: 34
Joined: 22 Sep 2014, 12:50

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

25 Jun 2019, 11:19

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...🤣

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests