{"id":109,"date":"2015-09-04T22:00:23","date_gmt":"2015-09-04T22:00:23","guid":{"rendered":"http:\/\/fluentreports.com\/blog\/?p=109"},"modified":"2016-01-02T05:07:28","modified_gmt":"2016-01-02T05:07:28","slug":"fonter-a-simple-nativescript-font-testing-application","status":"publish","type":"post","link":"http:\/\/fluentreports.com\/blog\/?p=109","title":{"rendered":"Fonter - A Simple NativeScript Font Application for iOS and Android"},"content":{"rendered":"<p>[[ A updated version of this post has been posted for NativeScript v1.5+ and icon fonts <a href=\"http:\/\/fluentreports.com\/blog\/?p=176\">here<\/a>. ]]<\/p>\n<p>Since the subject of Fonts has been causing issues for multiple people in the NativeScript community I figured I would write up a post on how to do it.<\/p>\n<p>Attached to this post is the completed project.\u00a0\u00a0\u00a0 First thing you need to do is a<br \/>\n<strong>tns create project fonter<\/strong> -- then cd fonter and <strong>tns platform add android<\/strong> or <strong>tns platform add ios<\/strong>.\u00a0\u00a0 Now your project is ready to go.\u00a0 I deleted the app\/main-page.js and the app\/main-view-model.js in this sample app, since they are not needed.<\/p>\n<p>Next thing we need are some fonts.\u00a0\u00a0 So I go to <a href=\"https:\/\/Google.com\/fonts\">https:\/\/Google.com\/fonts<\/a> and look at several fonts and pick a couple fonts and download them.\u00a0 (Please note you actually have to download the fonts from <a href=\"https:\/\/github.com\/google\/fonts\">https:\/\/github.com\/google\/fonts<\/a>)\u00a0\u00a0 To keep things simple, all three fonts are under the SIL Open Font License: <a href=\"http:\/\/scripts.sil.org\/OFL\">http:\/\/scripts.sil.org\/OFL<\/a><\/p>\n<p><a href=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/font-folder.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-110 size-full\" src=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/font-folder.png\" alt=\"font-folder\" width=\"272\" height=\"203\" \/><\/a><\/p>\n<p>Second thing we need to do navigate to your app folder, and create a new \"<strong>fonts<\/strong>\" folder like so:<\/p>\n<p>This folder MUST be named the exactly \"<strong>fonts<\/strong>\", no upper case letters.\u00a0 It must be all lower case letters, exactly \"<strong>fonts<\/strong>\".<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/font-list.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-113 alignright\" src=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/font-list.png\" alt=\"font-list\" width=\"270\" height=\"112\" \/><\/a><\/p>\n<p>The next thing we will do is copy the fonts you downloaded into your fonts folder.\u00a0\u00a0 In my case I downloaded <strong>Indie Flower<\/strong>, <strong>Josefin Sans<\/strong>, and <strong>Lobster<\/strong>.\u00a0 So my folder looks like this:<br \/>\nSo we have three fonts that we want to add to the CSS.\u00a0 In my case I want these fonts globally in the application so I will open up the app.css file; and add the three following changes:<br \/>\n<pre><code>\n.Lobster {\nfont-family: Lobster-Regular;\n}<\/code><\/pre><\/p>\n<p>.IndieFlower {<br \/>\nfont-family: IndieFlower;<br \/>\n}<\/p>\n<p>.JosefinSans {<br \/>\nfont-family: JosefinSans-Regular;<br \/>\n}<\/p>\n<p>You might notice; the font-family name is the <strong>EXACT SAME SPELLING<\/strong> and <strong>EXACT SAME CASE<\/strong> of each of the file names.\u00a0 The only thing that is removed is the <strong>.ttf<\/strong> extension.\u00a0\u00a0 This is <strong>REQUIRED <\/strong>for Android.\u00a0 Android will automatically load the file (with the .ttf) referenced in the <strong>font-family<\/strong> from the fonts folder.<\/p>\n<p>On iOS we have to do something slightly different; we have to register them before use.\u00a0 If you open up the app.js file, you need to add the following code to it, as you see just <strong>BEFORE <\/strong>the application.start().<\/p>\n<p><pre><code>\nif (application.ios) {\nvar fontModule = require(&quot;ui\/styling\/font&quot;);\nfontModule.ios.registerFont(&quot;IndieFlower.ttf&quot;);\nfontModule.ios.registerFont(&quot;JosefinSans-Regular.ttf&quot;);\nfontModule.ios.registerFont(&quot;Lobster-Regular.ttf&quot;);\n}\napplication.start();\n<\/code><\/pre><\/p>\n<p>Again, you will notice the file name is spelled and cased exactly the same as the file name on in your fonts folder.\u00a0\u00a0 This is important!\u00a0\u00a0\u00a0 However you name the file; both the iOS registration and the android css declarations MUST be exactly the same.<\/p>\n<p>At this moment, the fonts are registered and usable on both iOS and Android.\u00a0\u00a0 So lets show them.\u00a0\u00a0 Open up your main-page.xml and here is the code I used:<\/p>\n<p><pre><code>\n\n&lt;label&gt;&lt;\/label&gt;\n&lt;label&gt;&lt;\/label&gt;\n&lt;label&gt;&lt;\/label&gt;\n\n<\/code><\/pre><\/p>\n<p><a href=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/fonts-android.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-medium wp-image-111\" src=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/fonts-android-300x219.png\" alt=\"fonts-android\" width=\"300\" height=\"219\" srcset=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/fonts-android-300x219.png 300w, http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/fonts-android.png 564w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>So now we want to try the code.\u00a0 <strong>tns run ios --emulator<\/strong> or <strong>tns run android --emulator<\/strong>.\u00a0 On Android everything should work perfectly and you should see something like this.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Unfortunately on iOS it isn't as simple, when you run this project you will see that <strong>Lobster<\/strong> and <strong>Indie Flower<\/strong> work properly, but <strong>Josefin Sans<\/strong> is defaulting to the default font...<\/p>\n<p><a href=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/font-titlebar-osx.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-medium wp-image-117\" src=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/font-titlebar-osx-300x286.png\" alt=\"font-titlebar-osx\" width=\"300\" height=\"286\" srcset=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/font-titlebar-osx-300x286.png 300w, http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/font-titlebar-osx.png 399w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>Here is the \"small gotcha\" on iOS.\u00a0\u00a0 The font is registered under its actual font name, not under the file name.\u00a0\u00a0 So we have one more step you need to do; and you can do it in either OSX or Windows.\u00a0 Go to your fonts folder and double click on the <strong>JosefinSans-Regular.ttf<\/strong> and you should see a window like either of these (depending on your platform).\u00a0 Notice the part I highlighted in Red.\u00a0 That is the actual font name.\u00a0\u00a0 So, the last piece to this puzzle is you need re-open the app.css file and change the .JosefinSans class declaration to:<a href=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/font-titlebar-win.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-medium wp-image-118\" src=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/font-titlebar-win.png\" alt=\"font-titlebar-win\" width=\"299\" height=\"169\" \/><\/a><\/p>\n<p>.JosefinSans {<br \/>\nfont-family: JosefinSans-Regular<b>,Josefin Sans;<\/b><br \/>\n}<\/p>\n<p>Do you see what I added.\u00a0 I appended the actual font name to the font-family.\u00a0 The ORDER is important.\u00a0\u00a0 On Android because it does auto-registration of the fonts, it will load and register the <strong>FIRST<\/strong> font, and then it is happy.\u00a0\u00a0 On iOS it doesn't see the first font so it just ignores it and then sees the <strong>SECOND<\/strong> font and then it is happy.<\/p>\n<p><a href=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/fonts-ios.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-medium wp-image-112\" src=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/fonts-ios-300x197.png\" alt=\"fonts-ios\" width=\"300\" height=\"197\" srcset=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/fonts-ios-300x197.png 300w, http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/fonts-ios.png 480w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>So now when you do a <strong>tns run ios --emulator<\/strong> you should see this.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>You can download the complete project (including fonts) here: <a href=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2015\/09\/fonter.zip\">fonter.zip<\/a><\/p>\n<p>The fonts\/fonts.json file is just the font information for the fonts from the google code project; I wanted to make sure the copyrights information was in the same folder as the fonts in case someone finds the sample elsewhere.<\/p>\n<p>[[ A updated version of this post has been posted for NativeScript v1.5+ and icon fonts <a href=\"http:\/\/fluentreports.com\/blog\/?p=176\">here<\/a>. ]]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[[ A updated version of this post has been posted for NativeScript v1.5+ and icon fonts here. ]] Since the subject of Fonts has been causing issues for multiple people in the NativeScript community I figured I would write up a post on how to do it. Attached to this post is the completed project.\u00a0\u00a0\u00a0&hellip; <a class=\"more-link\" href=\"http:\/\/fluentreports.com\/blog\/?p=109\">Continue reading <span class=\"screen-reader-text\">Fonter - A Simple NativeScript Font Application for iOS and Android<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry"],"_links":{"self":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=109"}],"version-history":[{"count":13,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":187,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions\/187"}],"wp:attachment":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}