25

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?

3
  • I'm no expert, but maybe you should remove the / character in front of the word fonts/#fontName ? Commented Sep 22, 2010 at 1:32
  • Yeah tried that too. no luck. :-( Commented Sep 22, 2010 at 2:34
  • Tried "<TextBlock FontFamily="./fonts/#SpecialFont"" too. This font is installed in my system. However when i first tried "FontFamily="SpecialFont"" it didn't work either. Could this be something to do with the font? I've had no problems using this font in ASP.NET. Commented Sep 22, 2010 at 3:12

4 Answers 4

25

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

Sign up to request clarification or add additional context in comments.

3 Comments

Of course i read that! The link i've given in my question is to that page itself.
Thanks for that. But turns out it was something wrong with the way specified the font name.
" change the BuildAction to Resource" was the missing link for me. been trying for hours to figure this out until I read this thread.
13

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.

8 Comments

Yes build action's set to 'Resource'. This font is installed in my system. But even FontFamily="SpecialFont" doesn't work.
The fonts directory containing the font is placed in the root of your WPF application, I hope. I couldnt reproduce your error, its working fine here. Can you paste the entire markup here including how you have named the font?
Yeah the fonts directory is application/fonts/... Let me reedit my question and post the entire mark up.
Ok this is crazy. I just had a look at the collection returned from System.Windows.Media.Fonts.SystemFontFamilies. That has the font but with an 'x' at the end > 'FMBasuru x'. I used that and it works great. I thought what i need to use is the type face name like i've done in asp.net and winforms. Was i wrong? Sorry if i wasted all your time on something stupid!
Yeah, FMBasurux is a Sinhala font, it always requires the 'x' at the end.
|
4

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.