{"id":46895,"date":"2020-04-03T10:31:22","date_gmt":"2020-04-03T17:31:22","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/xamarin\/?p=46895"},"modified":"2020-04-23T01:23:12","modified_gmt":"2020-04-23T08:23:12","slug":"embedded-fonts-xamarin-forms","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/embedded-fonts-xamarin-forms\/","title":{"rendered":"Embedded Fonts: Custom Fonts in Xamarin.Forms"},"content":{"rendered":"<p>There are a number of <a href=\"https:\/\/blog.verslu.is\/xamarin\/xamarin-forms-xamarin\/using-custom-fonts-on-ios-and-android-with-xamarin-forms\/\" target=\"_blank\" rel=\"noopener noreferrer\">posts<\/a> about <a href=\"https:\/\/blog.verslu.is\/xamarin\/xamarin-forms-xamarin\/custom-fonts-with-xamarin-forms-revisited\/\" target=\"_blank\" rel=\"noopener noreferrer\">using custom fonts<\/a> in Xamarin.Forms. However, embedded fonts definitely takes the cake. No more platform-specific handling of fonts and adding font files in three different projects. Just one file and an attribute in your shared code and your font is ready to go. In this post we will see how to use it in Xamarin.Forms 4.5.530 and up.<\/p>\n<h2>Life Before Embedded Fonts<\/h2>\n<p>As mentioned, there are a couple of posts on using custom fonts already. The most recent one can be found <a href=\"https:\/\/blog.verslu.is\/xamarin\/xamarin-forms-xamarin\/custom-fonts-with-xamarin-forms-revisited\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>. In that post, you will see, that depending on the platform, you would need to go through some hoops. Such as adding an entry to the metadata or adding the font file to each target platform project. As well as setting the right build action, finding out the correct font name, and then cross your fingers everything was right the first time.<\/p>\n<p>Written down like this, it doesn&#8217;t seem like a lot of work and granted. You only have to do it once (per font), but it can still be a hassle. Especially the part about finding the correct font-family name (or postscript name), involves some trail-and-error. If you have worked with custom fonts before, you already know these embedded fonts will be a big improvement.<\/p>\n<h2>How To Use Embedded Fonts<\/h2>\n<p>As of Xamarin.Forms 4.5, we will make your life a bit easier yet again. In just three simple steps, we will add a font to use across iOS, Android and UWP.<\/p>\n<h3>1. Add the font file (otf or ttf) to your shared project and mark it as embedded resource<\/h3>\n<p>Simply add the font file <code>Samantha.ttf<\/code> to any folder in your project and set the Build Action to <code>EmbeddedResource<\/code>. In this case, find it <a href=\"https:\/\/www.dafont.com\/samantha-3.font\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<p><center><img decoding=\"async\" class=\"size-full wp-image-46906\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/04\/3QkbzlV1.png\" alt=\"Screenshot of Visual Studio for Mac showing how to set the font file build action to EmbeddedResource\" width=\"1188\" height=\"982\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/04\/3QkbzlV1.png 1188w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/04\/3QkbzlV1-300x248.png 300w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/04\/3QkbzlV1-1024x846.png 1024w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/04\/3QkbzlV1-768x635.png 768w\" sizes=\"(max-width: 1188px) 100vw, 1188px\" \/><\/center>&nbsp;<\/p>\n<h3>2. Add ExportFont attribute in your shared project<\/h3>\n<p>The most obvious place would be inside your <code>App.xaml.cs<\/code> or <code>AssemblyInfo.cs<\/code>. However, since the attribute will register this on assembly level, you can put this anywhere. The placement of the attribute depends on how visible you want to make it to other\/future developers.<\/p>\n<p>Anywhere outside a namespace will work. Just add this, you don&#8217;t need to include the subfolders:\n<code>[assembly: ExportFont(\"Samantha.ttf\")]<\/code><\/p>\n<h3>3. Consume the new font in your controls<\/h3>\n<p>That&#8217;s it! You are now ready to consume the font in your app. Through code or XAML, and directly on a control or through a style. Notice that you don&#8217;t need to find out the postscript font name anymore. Just use the fonts filename. Which will just work on all platforms!<\/p>\n<p>Note that adding an entry in your <code>info.plist<\/code> was not added for iOS. That is also not needed anymore.<\/p>\n<p>To use a more custom name, alternatively, use the Alias property. There you can specify a name to reference this font by. For example:<\/p>\n<pre><code>[assembly: ExportFont(\"Samantha.ttf\", Alias = \"MyAwesomeCustomFont\")]<\/code><\/pre>\n<p>Now reference this font by using <code>MyAwesomeCustomFont<\/code>.<\/p>\n<p>Here is an example of using it with a <code>Label<\/code>. Where it says <code>MyAwesomeCustomFont<\/code>, it could also have said <code>Samantha<\/code> when you want to simply use the filename.<\/p>\n<pre><code>&lt;Label Text=\"Welcome to Xamarin.Forms!\" \r\n    FontFamily=\"MyAwesomeCustomFont\" FontSize=\"50\" \r\n    HorizontalOptions=\"Center\" VerticalOptions=\"CenterAndExpand\" \/&gt;<\/code><\/pre>\n<p><center><img decoding=\"async\" class=\"size-full wp-image-46911\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/04\/aeZ94Faz.png\" alt=\"Screenshot of the iOS Simulator showing an example of custom font usage with embedded fonts on iOS\" width=\"924\" height=\"436\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/04\/aeZ94Faz.png 924w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/04\/aeZ94Faz-300x142.png 300w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/04\/aeZ94Faz-768x362.png 768w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/center>&nbsp;<\/p>\n<p>Under the hood, the font is loaded and cached whenever you start the app. Because of that, be mindful of how many custom fonts you are adding. Although, this is no different than how it works on the native platforms today.<\/p>\n<p>All the information on using custom fonts in Xamarin.Forms can also be found in <a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/xamarin-forms\/user-interface\/text\/fonts\" target=\"_blank\" rel=\"noopener noreferrer\">the docs<\/a>.<\/p>\n<h2>Recap of Embedding Fonts<\/h2>\n<p>In closing, the &#8220;old&#8221; way still works fine. Your code can remain exactly the same and nothing will change. In fact; if you have custom fonts in place right now, just leave it like that unless you have a good reason to change it. If you are starting a new project or want to tweak maintainability, embedded fonts is for you.<\/p>\n<p>We hope this functionality will make the experience when working with fonts much simpler. Needless to say, this also works with icon fonts like Font Awesome, seen in the sample project below.<\/p>\n<p>Want to learn more about what is new in the latest Xamarin.Forms release? Check out the <a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/xamarin-forms\/release-notes\/4.5\/4.5.0-sr3\" target=\"_blank\" rel=\"noopener noreferrer\">release notes<\/a>.<\/p>\n<p>Besides fonts there is more you can do to create stunning apps. Check out this earlier <a href=\"https:\/\/devblogs.microsoft.com\/xamarin\/snppts-ui-snippets-xamarin-forms\/\">post<\/a> about Snppts for pre-made designs. Maybe you want a set of controls ready to go, like <a href=\"https:\/\/devblogs.microsoft.com\/xamarin\/grial-ui-kit-xamarin-forms\/\">Grial Kit<\/a>. Now is the time to create beautiful apps with Forms.<\/p>\n<p>Take a look at the code used in this post in this GitHub Repo: <a href=\"https:\/\/github.com\/jfversluis\/EmbeddedFontsSample\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/jfversluis\/EmbeddedFontsSample<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Embedded fonts means no more platform-specific handling of fonts or adding font files in three different projects. In this post we will see how to use this in Xamarin.Forms 4.5.530 and up.<\/p>\n","protected":false},"author":13350,"featured_media":46911,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[367],"tags":[8402,8403,8102],"class_list":["post-46895","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-xamarin-forms","tag-embedded-fonts","tag-fonts","tag-xamarin-forms-4-5"],"acf":[],"blog_post_summary":"<p>Embedded fonts means no more platform-specific handling of fonts or adding font files in three different projects. In this post we will see how to use this in Xamarin.Forms 4.5.530 and up.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/46895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/13350"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=46895"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/46895\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/46911"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=46895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=46895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=46895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}