charri / Font-Awesome-WPF

FontAwesome controls for WPF+UWP
MIT License
530 stars 147 forks source link

[Question] Does the Nuget package include the font as resource? #11

Closed BrainCrumbz closed 9 years ago

BrainCrumbz commented 9 years ago

I'd like to use FontAwesome as a regular font resource, alike what's been done in this article or this SO thread.

Thus I was wondering if the installed Nuget package also includes the font as a resource. I checked the package folder and I can only wonder if FontAwesome.WPF.dll embeds the resource.

TY

charri commented 9 years ago

@BrainCrumbz yes it does.

Have a look at FontAwesome.cs

this code should work out of the box if you have the dll referenced

var font = new FontFamily(new Uri("pack://application:,,,/FontAwesome.WPF;component/"), "./#FontAwesome");

var text = new TextBlock();
text.FontFamily = font;

// set the icon you want to use in 'text' and add to a parent control
BrainCrumbz commented 9 years ago

Thanks for your reply. In the meantime I was using the downloaded TTF file, and I also went on with other tests.
Before I was trying to set FontFamily in code, to be used in a custom Behavior + Attached Dependency Property, with the idea of submit that to your repository. Then, tinkering more with code, I ditched the idea of Behaviour and I turned towards working with a Style + Attached Dependency Property.
This last approach now is working: I set FontFamily in a Style or anyway in XAML, and I use the attached dependency property to set the Content through a FontAwesomeIcon in XAML. The dependency property takes care of converting the Enum into string to feed the content.

<!-- the dependency prop. is defined at helpers:Awesome.Content -->

<Button helpers:Awesome.Content="LightbulbOutlined" 
        TextElement.FontFamily="...path to TTF resource.../#FontAwesome" />

I then tried to work with the DLL from the package, referencing that in Project, and using that URI path instead of the TTF one. At runtime, both tecnhiques works the same. But VS Designer is only able to display the symbols when I use the TTF. If I use the resource contained in DLL, the designer shows the empty square instead.

<Button helpers:Awesome.Content="LightbulbOutlined" 
        TextElement.FontFamily="pack://application:,,,/FontAwesome.WPF;component/#FontAwesome" />

Do you know why is that?

charri commented 9 years ago

I have the same issue with the controls I created. The Visual Studio Designer does not seem to take embedded fonts into account. The only way seems to install the font to the system work back with fallback values: have a look here