{"id":5231,"date":"2013-05-07T13:16:57","date_gmt":"2013-05-07T17:16:57","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=5231"},"modified":"2013-05-07T13:16:57","modified_gmt":"2013-05-07T17:16:57","slug":"custom-fonts-in-ios","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/custom-fonts-in-ios\/","title":{"rendered":"Using custom Fonts in Xamarin.iOS"},"content":{"rendered":"<p>\t\t\t\tApple introduced a great feature when it released the iOS 3.2 SDK way back in 2009: support for adding custom fonts to apps. Since then, developers have made their apps stand out by using different fonts.<\/p>\n<p>Although adding fonts should be a very simple process, developers often express confusion over how to do it for Xamarin.iOS apps. The process has a few &#8220;gotcha&#8217;s&#8221; that might catch you off guard if you skipped your morning coffee or you are trying to get a release out late at night.<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" class=\"size-large wp-image-5236 aligncenter\" alt=\"CustomIOSFonts\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/CustomIOSFonts-1024x588.png\" width=\"1024\" height=\"588\" \/><\/p>\n<p>In this blog post, I describe how to add a custom font to a label. This should give you an understanding of the process and help you if you&#8217;re stuck.<\/p>\n<p>First, find yourself a font with a suitable license. I use <a href=\"http:\/\/dafont.com\">dafont.com<\/a>, a great resource for finding awesome-looking fonts. You can quickly find free fonts that you can use in your application at no cost; however, if you find a premium font you like, you may be able to license it. In this example, I am going to use a free font titled HollywoodHills.ttf. You can download a copy yourself if you wish to follow along.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/www.dafont.com\/hollywood-hills.font\"><img decoding=\"async\" class=\"size-medium wp-image-5237 aligncenter\" alt=\"FontSample\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/FontSample-300x79.png\" width=\"300\" height=\"79\" \/><\/a><\/p>\n<p>When searching\u00a0for suitable fonts, it&#8217;s worth remembering that iOS only supports TTF and OTF. This shouldn&#8217;t be a problem as most fonts available online come in these two formats.<\/p>\n<h3>Add the font<\/h3>\n<p>You should add the font to your project by either dragging and dropping it to a folder or right clicking and selecting &#8216;Add existing file.&#8217; In this example, I have added the font to the Resources folder. You can, of course, add the font to any folder\u2014including the root directory if you so choose.<\/p>\n<p>Once you&#8217;ve added the font, you should then right click it and select the &#8216;Properties&#8217; menu option. You will need to change the build property \u2018Copy to output directory\u2019 to \u2018Always copy.&#8217; If you forget to do this, you will get a null exception error when trying to reference the font at runtime. This will cause your App to crash.<\/p>\n<p><figure id=\"attachment_5238\" aria-labelledby=\"figcaption_attachment_5238\" class=\"wp-caption alignright\" ><img decoding=\"async\" class=\"size-medium wp-image-5238\" alt=\"fontProperties\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/fontProperties-233x300.png\" width=\"233\" height=\"300\" \/><figcaption id=\"figcaption_attachment_5238\" class=\"wp-caption-text\">Highlighted property for clarity<\/figcaption><\/figure><\/p>\n<h3>Info.plist<\/h3>\n<p>The next step is to tell iOS where your custom font is stored. iOS will load these fonts at startup, so it pays to use custom fonts sparingly\u2014it can slow the startup time of your App.<\/p>\n<p>To tell iOS you are using custom fonts, you should open the Info.plist file and select \u2018Source\u2019 at the bottom of the view. This will change the UI to be more inline with Xcode&#8217;s property editor.<\/p>\n<p>Once you&#8217;ve done this, you should then double click on \u2018Add new entry\u2019 and select the \u2018Fonts provided by application\u2019 option. It is here that you input the location of the font so that iOS knows where to look when loading the fonts at startup. Because I have put the font in Resources, I can simply type &#8216;HollywoodHills.ttf&#8217; as the value. If I had put the font in Resources\/Fonts, then I would set the value to &#8216;Fonts\/HollywoodHills.ttf&#8217;.<\/p>\n<h3>Using the custom font<\/h3>\n<p>Now comes the interesting part: using the font. Let&#8217;s create two labels, one with our custom font and the other with the default. I&#8217;ve included code below that will make and place these labels for you. You can copy and paste the following code into your application&#8217;s ViewDidLoad method. Try running the app to see how it looks. If you configured your project as I described above, it should work with no issues.<\/p>\n<pre class=\"lang:csharp decode:true\">\n\/\/Get size of screen\nvar height = UIScreen.MainScreen.Bounds.Height;\nvar width = UIScreen.MainScreen.Bounds.Width;\n\n\/\/Labels\nvar lab1 = new UILabel(new RectangleF(0,0, width, height \/2));\nlab1.Text = &quot;This is some sample text&quot;;\nlab1.Font = UIFont.FromName(&quot;HollywoodHills&quot;, 20f);\nthis.Add(lab1);\n\nvar lab2 = new UILabel(new RectangleF(0,height \/2, width, height \/2));\nlab2.Text = &quot;This is some more sample text&quot;;\nthis.Add(lab2);\n<\/pre>\n<p>When you run your App, you will now see that the top label uses the custom font you downloaded from Dafont. It&#8217;s as easy as that. If you are having problems, you should double-check that you have selected the \u2018Always copy\u2019 build property for the font and that the path is correct in the Info.plist.<\/p>\n<p>I hope you enjoy using custom fonts in Apps as much as I have.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apple introduced a great feature when it released the iOS 3.2 SDK way back in 2009: support for adding custom fonts to apps. Since then, developers have made their apps stand out by using different fonts. Although adding fonts should be a very simple process, developers often express confusion over how to do it for [&hellip;]<\/p>\n","protected":false},"author":1929,"featured_media":39167,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[6,4],"class_list":["post-5231","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-ios","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>Apple introduced a great feature when it released the iOS 3.2 SDK way back in 2009: support for adding custom fonts to apps. Since then, developers have made their apps stand out by using different fonts. Although adding fonts should be a very simple process, developers often express confusion over how to do it for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/5231","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\/1929"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=5231"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/5231\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=5231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=5231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=5231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}