{"id":301,"date":"2015-09-23T22:25:12","date_gmt":"2015-09-23T22:25:12","guid":{"rendered":"http:\/\/strawberrycode.com\/blog\/?p=301"},"modified":"2015-09-23T22:25:12","modified_gmt":"2015-09-23T22:25:12","slug":"uifont-not-responding-or-nil","status":"publish","type":"post","link":"https:\/\/strawberrycode.com\/blog\/uifont-not-responding-or-nil\/","title":{"rendered":"UIFont not responding or nil"},"content":{"rendered":"<p>My font does not respond and it says <code>nil<\/code> when I print it? What&#8217;s happening here?<br \/>\nActually, if your <code>UIFont<\/code> happens to be <code>nil<\/code>, it&#8217;s maybe because you&#8217;re not calling it properly! Haha, very funny, I know, but let&#8217;s have a look at happened to me recently.<\/p>\n<p>Here is what I did:<br \/>\nI imported <code>\"MyFont-Regular.ttf\"<\/code> and <code>\"MyFont-bold.ttf\"<\/code> in Xcode.<br \/>\nI set the constants with the font name.<\/p>\n<pre class=\"lang:swift decode:true \">let fontRegular = \"MyFont-Regular\"\r\nlet fontBold = \"MyFont-Bold\"<\/pre>\n<p>And I set the font to that label in the view:<\/p>\n<pre class=\"lang:swift decode:true \">label.font = UIFont(name: fontRegular, size: 12.0)<\/pre>\n<p>and &#8230; nothing happened, well, the label was\u00a0displayed but not as wanted. I expected something like <code>label2<\/code> and <code>label1<\/code> happened. And <code>Label3<\/code> worked just fine btw&#8230;<\/p>\n<figure id=\"attachment_304\" aria-describedby=\"caption-attachment-304\" style=\"width: 504px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2015\/09\/UIFont_labels.png\"><img loading=\"lazy\" class=\"wp-image-304 size-full\" src=\"http:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2015\/09\/UIFont_labels.png\" alt=\"UIFont Labels example\" width=\"504\" height=\"292\" srcset=\"https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2015\/09\/UIFont_labels.png 504w, https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2015\/09\/UIFont_labels-300x174.png 300w\" sizes=\"(max-width: 504px) 100vw, 504px\" \/><\/a><figcaption id=\"caption-attachment-304\" class=\"wp-caption-text\">Label 1, 2 and 3&#8230; fail, expected and success<\/figcaption><\/figure>\n<p>I printed\u00a0the <code>UIFont<\/code>\u00a0and it failed on that line!\u00a0So I double checked if my fonts were loaded correctly. And they were, so the problem was somewhere else.<\/p>\n<p>Then I remembered I saw something about printing the available fonts in this article: <a href=\"https:\/\/grokswift.com\/custom-fonts\/\" target=\"_blank\">Custom Fonts in Swift<\/a>.<\/p>\n<p>Those few lines can save you a lot of time and trouble:<\/p>\n<pre class=\"lang:swift decode:true\">func printMyFonts() {\r\n    print(\"--------- Available Font names ----------\")\r\n\r\n    for name in UIFont.familyNames() {\r\n        print(name)\r\n        if let nameString = name as? String {\r\n            print(UIFont.fontNamesForFamilyName(nameString))\r\n        }\r\n    }\r\n}<\/pre>\n<p>After running the little <code>PrintMyFonts()<\/code> function, I realized that the font I needed was called <code>\"MyFont\"<\/code> by the system and not <code>\"MyFont-Regular\"<\/code>. Aha!<\/p>\n<p><a href=\"http:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2015\/09\/facepalm.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-307\" src=\"http:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2015\/09\/facepalm.jpg\" alt=\"Facepalm\" width=\"300\" height=\"190\" \/><\/a><\/p>\n<p>That sounds maybe trivial, but I hope this will help you avoiding headaches and pants-over-head frustration. And remember: <strong>do not assume<\/strong> the font name based on the file name, even if it works sometimes ;)<\/p>\n<p><strong>Bonus:<\/strong> the list of default fonts supported on iOS: <a href=\"http:\/\/iosfonts.com\/\" target=\"_blank\">iOSFonts.com<\/a>. It lists every font for every iOS version, for iPhone, iPad and \uf8ffWatch, pretty neat!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My font does not respond and it says nil when I print it? What&#8217;s happening here? Actually, if your UIFont happens to be nil, it&#8217;s maybe because you&#8217;re not calling it properly! Haha, very funny, I know, but let&#8217;s have a look at happened to me recently. Here is what I did: I imported &#8220;MyFont-Regular.ttf&#8221; and &#8220;MyFont-bold.ttf&#8221; in Xcode. I set the constants with the font name. let fontRegular = &#8220;MyFont-Regular&#8221; let fontBold = &#8220;MyFont-Bold&#8221; And I set the font to that label in the view: label.font = UIFont(name: fontRegular, size: 12.0) and &#8230; nothing happened, well, the label was\u00a0displayed &hellip; <a href=\"https:\/\/strawberrycode.com\/blog\/uifont-not-responding-or-nil\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">UIFont not responding or nil<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":183,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[40],"tags":[60,17,61,59,62],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.1.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>UIFont not responding or nil - StrawberryCode<\/title>\n<meta name=\"description\" content=\"The UIFont does not respond and it says nil when I print it? It&#039;s maybe because you&#039;re not calling it properly! Try to print the available font names.\" \/>\n<link rel=\"canonical\" href=\"https:\/\/strawberrycode.com\/blog\/uifont-not-responding-or-nil\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"UIFont not responding or nil - StrawberryCode\" \/>\n<meta property=\"og:description\" content=\"The UIFont does not respond and it says nil when I print it? It&#039;s maybe because you&#039;re not calling it properly! Try to print the available font names.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/strawberrycode.com\/blog\/uifont-not-responding-or-nil\/\" \/>\n<meta property=\"og:site_name\" content=\"StrawberryCode\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-23T22:25:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2015\/04\/Apple_Swift_Logo-e1430073805246.png\" \/>\n\t<meta property=\"og:image:width\" content=\"200\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\">\n\t<meta name=\"twitter:data1\" content=\"1 minute\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/#website\",\"url\":\"https:\/\/strawberrycode.com\/blog\/\",\"name\":\"StrawberryCode\",\"description\":\"Fruit for Thought\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/strawberrycode.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/uifont-not-responding-or-nil\/#primaryimage\",\"inLanguage\":\"en-GB\",\"url\":\"https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2015\/04\/Apple_Swift_Logo-e1430073805246.png\",\"contentUrl\":\"https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2015\/04\/Apple_Swift_Logo-e1430073805246.png\",\"width\":200,\"height\":200,\"caption\":\"Apple Swift Logo\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/uifont-not-responding-or-nil\/#webpage\",\"url\":\"https:\/\/strawberrycode.com\/blog\/uifont-not-responding-or-nil\/\",\"name\":\"UIFont not responding or nil - StrawberryCode\",\"isPartOf\":{\"@id\":\"https:\/\/strawberrycode.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/strawberrycode.com\/blog\/uifont-not-responding-or-nil\/#primaryimage\"},\"datePublished\":\"2015-09-23T22:25:12+00:00\",\"dateModified\":\"2015-09-23T22:25:12+00:00\",\"author\":{\"@id\":\"https:\/\/strawberrycode.com\/blog\/#\/schema\/person\/c328d959959928f47281d7a0ec779e2a\"},\"description\":\"The UIFont does not respond and it says nil when I print it? It's maybe because you're not calling it properly! Try to print the available font names.\",\"breadcrumb\":{\"@id\":\"https:\/\/strawberrycode.com\/blog\/uifont-not-responding-or-nil\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/strawberrycode.com\/blog\/uifont-not-responding-or-nil\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/uifont-not-responding-or-nil\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/\",\"url\":\"https:\/\/strawberrycode.com\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/uifont-not-responding-or-nil\/\",\"url\":\"https:\/\/strawberrycode.com\/blog\/uifont-not-responding-or-nil\/\",\"name\":\"UIFont not responding or nil\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/#\/schema\/person\/c328d959959928f47281d7a0ec779e2a\",\"name\":\"StrawberryCode\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/301"}],"collection":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/comments?post=301"}],"version-history":[{"count":11,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/301\/revisions"}],"predecessor-version":[{"id":314,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/301\/revisions\/314"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/media\/183"}],"wp:attachment":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/media?parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/categories?post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/tags?post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}