xaml 언어별 폰트 추가 문의드립니다

WPF로 프로그램 개발중 언어별로 폰트를 다르게 해야하는 상황이 발생했습니다

언어별 버튼 클릭시 원하는 폰트로 변경하고 싶은데 가능한가요?

<Border>
  <Button Command="{Binding Path=ChangeLanguageCommand}" CommandParameter="en-US" BorderBrush="{x:Null}" Content="English" />
 </Border>
 <Border>
  <Button Command="{Binding Path=ChangeLanguageCommand}" CommandParameter="ja-JP" BorderBrush="{x:Null}" Content="日本語" />
 </Border>

private void ChangeLanguageExe(object obj)
{
     string lan = obj as string;

     LanguageHelper.LoadLanguageFile(lan);

     CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(lan);
     CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(lan);
}

public class LanguageChange
{
  public static void LoadLanguageFile(string languagefileName)
  {
    Application.Current.Resources.MergedDictionaries[0] = new ResourceDictionary()
    {
     Source = new Uri(String.Format("Language/{0}.xaml", languagefileName), UriKind.RelativeOrAbsolute)
    };
 }
}

이런식으로 언어별 데이터를 가지고 오는데 버튼클릭시 폰트 설정이 가능한지 알고 싶습니다

아래 글을 참고해 보시겠어요?

https://ikriv.com/dev/wpf/TextStyle/

1개의 좋아요

@dimohy 님이 링크해주신 사이트도 참고하시고
App.xaml에 <FontFamily x:Key="AppFontFamily"/> 하나 선언해주고 적용 시킬 컨트롤을 해당 AppFontFamily로 적용 시켜주면 될 것 같습니다.
실제 폰트를 바꾸는건 LanguageHelper에서 하시면 될 듯 합니다.

App.xaml

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                
            </ResourceDictionary.MergedDictionaries>

            <FontFamily x:Key="AppFontFamily"/>

            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="FontFamily" Value="{DynamicResource AppFontFamily}"/>
            </Style>

            <Style TargetType="TextBox">
                <Setter Property="FontFamily" Value="{DynamicResource AppFontFamily}"/>
            </Style>

            <Style TargetType="Button">
                <Setter Property="FontFamily" Value="{DynamicResource AppFontFamily}"/>
            </Style>
        </ResourceDictionary>

    </Application.Resources>
</Application>

LanguageHelper.cs
 public static void LoadLanguageFile(string languagefileName)
        {
            if (languagefileName == "en-US")
            {
                Application.Current.Resources["AppFontFamily"] = new FontFamily("Bodoni MT Negreta");
            }
            else if (languagefileName == "ja-JP")
            {
                Application.Current.Resources["AppFontFamily"] = new FontFamily("Arial");
            }
        }
1개의 좋아요

답변 감사합니다 혹시 폰트파일을 적용할려면

 if (languagefileName == "en-US")
            {
                Application.Current.Resources["AppFontFamily"] = new FontFamily("/Common/Fonts/NotoSans.otf");
            }
            else if (languagefileName == "ja-JP")
            {
                Application.Current.Resources["AppFontFamily"] = new FontFamily("/Common/Fonts/Roboto.ttf");
            }

이렇게 하는게 맞나요?

1개의 좋아요

글꼴을 설치하셨다면 윈도우에서 Font를 검색하고 사용 가능한 글꼴에 Roboto를 검색해보면 설치된 글꼴이 나옵니다. 밑줄친 이름을 넣어주면 됩니다.

글꼴은 정하셔야 할겁니다. Roboto도
Roboto-Bold, Roboto-Light, Roboto-Regular 등등 여러가지가 있네요.

1
2

1개의 좋아요

답변 감사합니다 혹시 Roboto-Bold, Roboto-Light, Roboto-Regular 모두 사용할수 있는 방법은 없을까요 화면에 따라서 원하는 글꼴을 사용하고 싶습니다

1개의 좋아요

일단 저는 폰트 설치 안하고 리소스로 넣어줬습니다.

1

App.xaml 에 추가로 원하는 폰트 추가해주시고

App.xaml

 <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                
            </ResourceDictionary.MergedDictionaries>

            <FontFamily x:Key="AppFontFamilyLight" />
            <FontFamily x:Key="AppFontFamilyRegular"/>
            <FontFamily x:Key="AppFontFamilyBlack"/>

            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="FontFamily" Value="{DynamicResource AppFontFamilyLight}"/>
            </Style>

            <Style TargetType="TextBox">
                <Setter Property="FontFamily" Value="{DynamicResource AppFontFamilyLight}"/>
            </Style>

            <Style TargetType="Button">
                <Setter Property="FontFamily" Value="{DynamicResource AppFontFamilyLight}"/>
            </Style>
        </ResourceDictionary>

    </Application.Resources>

LanguageHelper.cs

if (languagefileName == "en-US")
            {
                Application.Current.Resources["AppFontFamilyLight"] = new FontFamily(new Uri("pack://application:,,,/Roboto/"), "./#Roboto Light");
                Application.Current.Resources["AppFontFamilyRegular"] = new FontFamily(new Uri("pack://application:,,,/Roboto/"), "./#Roboto Regular");
                Application.Current.Resources["AppFontFamilyBlack"] = new FontFamily(new Uri("pack://application:,,,/Roboto/"), "./#Roboto Black");
            }
            else if (languagefileName == "ja-JP")
            {
                Application.Current.Resources["AppFontFamilyLight"] = new FontFamily(new Uri("pack://application:,,,/NotoSans/"), "./#Noto Sans KR Light");
                Application.Current.Resources["AppFontFamilyRegular"] = new FontFamily(new Uri("pack://application:,,,/NotoSans/"), "./#Noto Sans KR Regular");
                Application.Current.Resources["AppFontFamilyBlack"] = new FontFamily(new Uri("pack://application:,,,/NotoSans/"), "./#Noto Sans KR Black");
            }

적용 시키고 싶은 컨트롤에 적용 시키시면 될 것 같습니다.

Page.xaml
 <StackPanel HorizontalAlignment="Center"
                    VerticalAlignment="Center">

            <TextBlock Text="FontFamily Light"/>

            <Grid Margin="0,5"/>

            <TextBlock Text="FontFamily Regular"
                       FontFamily="{DynamicResource AppFontFamilyRegular}"/>

            <Grid Margin="0,5"/>

            <TextBlock Text="FontFamily Black"
                       FontFamily="{DynamicResource AppFontFamilyBlack}"/>

        </StackPanel>

2
3

글꼴 이름은 폴더가서 폰트 더블클릭 해보시면 나옵니다.
5

2개의 좋아요

정말 감사합니다 알려주신대로 하면 문제가 해결될것 같네요 진짜 죄송하지만 한가지만 더 질문드리겠습니다 버튼을 눌렀을때 글꼴이 바뀌는거 말고 프로그램이 시작할때 디폴트로 글꼴을 세팅 할수도 있나요?

1개의 좋아요

기본 설정은 App 시작할때 넣어주면 되지 않을까요?

App.xaml.cs

using System;
using System.Windows;
using System.Windows.Media;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            Application.Current.Resources["AppFontFamilyLight"] = new FontFamily(new Uri("pack://application:,,,/Roboto/"), "./#Roboto Light");
            Application.Current.Resources["AppFontFamilyRegular"] = new FontFamily(new Uri("pack://application:,,,/Roboto/"), "./#Roboto Regular");
            Application.Current.Resources["AppFontFamilyBlack"] = new FontFamily(new Uri("pack://application:,,,/Roboto/"), "./#Roboto Black");
        }
    }
}
2개의 좋아요

답변 감사합니다!!

1개의 좋아요