Prabath Yapa
Prabath Yapa

Reputation: 1229

Using a custom font in WPF

I need my WPF app to use a true-type font for a different language. I have the font located in a folder called 'fonts' inside the project. The font I'm using is available for free download here

Since the font is installed in my system i first tried

 FontFamily="FMBasuru"

I've read the post here and tried doing (this is the exact markup I'm using including font name)

<Window.Resources>
        <Style x:Key="SinhalaFont">
            <Setter Property="TextElement.FontFamily" Value="fonts/#FMBasuru"/>
        </Style>
    </Window.Resources>

...

 <TextBlock  Style="{DynamicResource SinhalaFont}">r</TextBlock>

...

I made sure that I'm using the correct font name instead of the font filename. What could have I got wrong?

Upvotes: 25

Views: 43501

Answers (4)

Kishore Kumar
Kishore Kumar

Reputation: 21873

Updated: Create a folder name Fonts and copy the font which you want and change the BuildAction to Resource

<Window.Resources>
    <FontFamily x:Key="test" >/Fonts/#Pirulen</FontFamily>
</Window.Resources>
<Grid>
    <TextBlock FontSize="25" HorizontalAlignment="Center" 
               FontFamily="{StaticResource test}">Kishore Kumar</TextBlock>
</Grid>

just refer this document

WPF - Add Custom Font

Upvotes: 24

Raz Luvaton
Raz Luvaton

Reputation: 3790

To add a custom font to your application

It's worked for me on Blend, I don't know about Visual Studio.

  1. In an open project in Expression Blend, under Files in the Project panel, right-click your project name, and then click Add Existing Item. enter image description here
  2. Browse to the custom font file (typically with a .ttf file name extension), select the custom font file so that it appears in the File text box, and then click Open. The custom font file is added to your application and appears under Files in the Project panel. enter image description here
  3. You can now embed the complete font or a subset the font in your application, and apply the font to text controls in your application.

The font will be in the font list

enter image description here

From - Add a custom font to your application

Upvotes: 0

Tharindu
Tharindu

Reputation: 101

Without using style, you can simply add the font like this in the Window.xaml I included the font file inside the folder called "Fonts".

<Window

 FontFamily ="./Fonts/#Arial"

>

And if you want to use another font for specific label or text block you can override it like this. You should insert the font file into the Fonts folder.

<TextBlock FontFamily = "./Fonts/#Tahoma" ></TextBlock>

Upvotes: 4

Mamta D
Mamta D

Reputation: 6460

I tried your code with this

  <Setter Property="TextElement.FontFamily" Value="fonts/#Arial Narrow Bold"/>

and it worked successfully.

Have you marked your font as 'Resource' in the Build Action? If you haven't, do that now and try your code again.

Upvotes: 13

Related Questions