I'm creating a WPF application and trying to match the style of the user interface to an existing web site. The site is using Lato as its default font, which is not available by default on Windows. I'd like to use Lato, but I can't require users to install the font. Is there any way to get WPF to use the font definition that is available through the Google Web Font service, or otherwise set this up so that a manual install of the font is not necessary?
Sure you can, the easy way is as a font resource you compile into your application:
First, download the TTF from google web fonts. From your example link find the link "Open Lato in Google Web Fonts" at the bottom of the page. This adds the font to your "collection". In the resulting page, up near the top is a smaller link titled "Download Your Collection" which will provide you with the the TTF font files in a zip file.
Next, embed the fonts as a resource in your WPF application. Add the files to your project, and make sure you select "Resource" for the compile action. Then, you can use your font in XAML simply by changing the FontFamily
property to the resource name rather than the font name: <TextBlock FontFamily="/Resources/#Lato">Lato Text Here</TextBlock>
. See the MSDN article on embedding fonts for more information.
You could also download the font on demand in code by setting the FontFamily
property of whatever element or container you want to a new FontFamily
object - use the constructor that takes a URI and use the URI of the TTF. I don't believe you can use the Google urls to do this directly, as the Google API assumes web page access and provides script and/or CSS as well as a font. Also note that this may require some additional permissions from what your app already demands.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With