[WPF] Font적용하기

C#/WPF 2022. 5. 14. 22:46
728x90
728x170

사용하고자할 Font 파일을 다운로드한다.
프로젝트에 Fonts 폴더를 만들고 다운로드한 폰트파일을 넣는다.

폰트의 속성창에서 아래처럼 리소스(Resource)를 선택한다.


화면의 xaml 단에아래처럼 정의해서 쓰면 된다.
FontFamily="/Wpf.FontTest;component/Fonts/#Donoun"

<Window x:Class="Wpf.FontTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        FontFamily="/Wpf.FontTest;component/Fonts/#Donoun"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <Label  Content="다람쥐 헌 쳇바퀴에 타고파. 폰트 적용 예제(Donoun - 전체적용)" FontSize="25" />
        <TextBlock Text="다람쥐 헌 쳇바퀴에 타고파. 폰트 적용 예제(Noto Sans)" FontSize="25" FontFamily="/Wpf.FontTest;component/Fonts/#Noto Sans CJK KR"/>
        <TextBox   Text="다람쥐 헌 쳇바퀴에 타고파. 폰트 적용 예제(고딕)" FontSize="25" FontFamily="고딕"/>
        <Label  Content="다람쥐 헌 쳇바퀴에 타고파. 폰트 적용 예제(Donoun - 전체적용)" FontSize="25" />
    </StackPanel>
</Window>

여기서 중요한건 폰트명에 #을 붙이고 폰트 파일명을 넣는게 아니라 폰트이름을 넣어야하는데
이는 글꼴을 클릭하면 아래와 같은 창에서 글꼴 이름 을 넣어줘야한다.

그런데 아래처럼 간혹 글꼴이름이 한글로 나올때가 있는데

이때는 아래 코드를 통해 영어 명칭을 알아내 영어 명칭으로 넣어주어야한다.

/// <summary>
/// 설치된 글꼴 명칭 확인
/// </summary>
private void FontList()
{
    List<string> fontList = new List<string>();
    foreach (FontFamily font in Fonts.SystemFontFamilies)
    {
        fontList.Add(string.Join(",", font.FamilyNames.Values));
        Debug.WriteLine(string.Join(",", font.FamilyNames.Values));
    }
}

소스
https://github.com/kei-soft/KJunBlog/tree/master/Wpf.FontTest

728x90
그리드형
Posted by kjun
,