Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get font name from FontFamily in WPF

I'm currently working on a little font organization/preview application for myself, however, I'm having a hard time getting the exact information I need.

I've found that I can load an external font by just creating a new FontFamily object with the font's file location as its source. However, I can't find a way to get a font's specific font name back. I know I can use FontFamily.FamilyNames to get the font's family name back, but that's useless to me when I have multiple fonts with the same family being displayed. I'd like to actually display the specific name for the specific font.

Is there any way to do this? I currently display the file name instead, but it's incredibly sloppy because I have to iterate through every file in a directory and call Fonts.GetFontFamilies() on each just so I can get the actual file name(FontFamily's Source property only gives WPF's makeshift family-name source instead of something useful).

like image 774
rossisdead Avatar asked Oct 27 '09 01:10

rossisdead


People also ask

How do I use font family in WPF?

WPF starts by matching the supplied FontFamily against the names of the fonts found on the system. It then tries to find a font that most closely matches the requested FontStretch, FontStyle and FontWeight property values. Matching FontStretch is the highest priority, followed by FontStyle and then FontWeight.

How do I change the font family in XAML?

Basically you override the meta data properties which merges in your changes to the existing values. TextElement. FontFamilyProperty. OverrideMetadata( typeof(TextElement), new FrameworkPropertyMetadata( new FontFamily("Comic Sans MS"))); TextBlock.

What is font family in c#?

FontFamily describes a specific family of fonts, such as the "Times New Roman" family. No style, size, or other information is part of the FontFamily. Info In the Font constructor, we specify the FontStyle using the bitwise OR operator. This is used on enums with the Flags attribute.


1 Answers

This is what I am doing:

        ListBoxItem listBoxItem = null;
        foreach (FontFamily fontFamily in Fonts.SystemFontFamilies)
        {
            listBoxItem = new ListBoxItem();
            listBoxItem.Content = fontFamily;
            listBoxItem.FontFamily=fontFamily; // Shows Font Text in the Font
            FontFamilyListBox.Items.Add(listBoxItem);
        }
like image 183
Satya Avatar answered Oct 16 '22 05:10

Satya