locked
Custom font for label RRS feed

  • Question

  • I would like to display some label in custom font for which I have *.ttf file. So I added this file to solution to 'Resources' folder with build action set as a resource. Font`s name is Katana. Then I tried to set FontFamily of my label in this way:

    <Label FontFamily="./Resources/#Katana">Importing data...</Label>

    I tried many different combinations of that string but none of them worked. How to do that?

    Thank you for help
    MCP
    Tuesday, January 26, 2010 6:45 AM

Answers

  • hi Martinez,
                   I have followed what you mentioned. Basically in window2 inside the new folder you have to use FontFamily="../#Officer Down"

    So what we ca deduce from this is that fonts added in the project resource should be accessed by their relative path if they are not in the same level.

    • Marked as answer by Martinez999 Tuesday, January 26, 2010 9:06 PM
    Tuesday, January 26, 2010 8:38 PM
  • yes i got it. Heres how :

    FontFamily="pack://application:,,,/#Officer Down"
    • Marked as answer by Martinez999 Tuesday, January 26, 2010 10:03 PM
    Tuesday, January 26, 2010 9:20 PM

All replies

  • Perhaps you can take a look at this link: http://msdn.microsoft.com/en-us/library/ms753303.aspx
    William
    Tuesday, January 26, 2010 6:57 AM
  • I already looked at that page - that`s from I took my example. But still I can`t figure out what`s wrong.
    MCP
    Tuesday, January 26, 2010 8:04 AM
  • You may want to check the name of the font, you need to specify the name of the font not the name of the file.

    Double click on the font file and it should show a "Font name:" that's what you want to make sure is specified in your style.

    Tuesday, January 26, 2010 9:36 AM
  • I have uploaded a sample application for you at : http://dl.dropbox.com/u/3692928/WpfApplication1.zip

    Here are the steps to use custom fonts :

    1. Go to your project properties and go to Resource Tab.
    2. Add the ttf file as resource.
    3. use it in you xaml as you can in the sample application.

    • Proposed as answer by Saraf Talukder Tuesday, January 26, 2010 10:02 AM
    • Unproposed as answer by Martinez999 Tuesday, January 26, 2010 10:08 AM
    Tuesday, January 26, 2010 10:02 AM
  • 1. I`m sure I`m using name of the font, not the name of the file.

    2. What this application has to do with my problem?

    According to Microsoft, I should be able to add my font file into the project in Solution Explorer, set it build action to Resource and then use it in my application using this code: FontFamily="./Resources/#Katana"
    MCP
    Tuesday, January 26, 2010 10:11 AM
  • do not use is as : FontFamily="./Resources/#Katana"

    use as : FontFamily="./#Katana"
    Tuesday, January 26, 2010 2:40 PM
  • the sample application i gave uses a customs font for a label. So you can run it and check.
    Tuesday, January 26, 2010 2:41 PM
  • 1. I gotta use path "./Resources/#Katana" since this font is placed in my Resources folder in solution explorer.
    2. There is no custom font for label in your app. Maybe there is some mistake with it.

    MCP
    Tuesday, January 26, 2010 3:25 PM
  • opps, i gave the wrong link by mistake. please check this : http://dl.dropbox.com/u/3692928/Articles%20Projects/CustomFontTest.zip
    Tuesday, January 26, 2010 4:24 PM
  • Ok thank you.

    I did exactly the same code as you. I hunted down for a difference between my and your app and I found very interesting thing. Please do as follows:

    1. In Solution Explorer add some folder to the main branch
    2. In this folder add new Window, let`s say Window2.xaml
    3. Now add a button on the main window and in click handler show the Window2, something like that:

    private void button1_Click(object sender, RoutedEventArgs e)
            {
                new foldername.Window2().Show();
            }

    It should result in default font used in TextBlock in Window2, although your custom font works well for Window1.

    So there must be some mistakes in path. I believe the dot "." does not point to standard folder where *.exe resides but to the folder where current Window resides. Do you have any idea how to solve it?
    MCP
    Tuesday, January 26, 2010 4:47 PM
  • hi Martinez,
                   I have followed what you mentioned. Basically in window2 inside the new folder you have to use FontFamily="../#Officer Down"

    So what we ca deduce from this is that fonts added in the project resource should be accessed by their relative path if they are not in the same level.

    • Marked as answer by Martinez999 Tuesday, January 26, 2010 9:06 PM
    Tuesday, January 26, 2010 8:38 PM
  • Yes exactly. Nevertheless such code is not very flexible. Do you know whether it is possible to use an absolute path from XAML?
    MCP
    Tuesday, January 26, 2010 9:02 PM
  • i am afraid you cannot when it comes to fonts. But i will have a look at it and if there is any way out for that , i will ping you back :)
    Tuesday, January 26, 2010 9:18 PM
  • yes i got it. Heres how :

    FontFamily="pack://application:,,,/#Officer Down"
    • Marked as answer by Martinez999 Tuesday, January 26, 2010 10:03 PM
    Tuesday, January 26, 2010 9:20 PM
  • see this for more information on uri's in wpf :

    http://msdn.microsoft.com/en-us/library/aa970069.aspx
    Tuesday, January 26, 2010 9:22 PM
  • Thank you very much, it was exactly what I asked for!
    Tuesday, January 26, 2010 9:22 PM