{"id":2462,"date":"2022-08-30T15:25:04","date_gmt":"2022-08-30T15:25:04","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/17\/fonts-added-at-runtime-in-ios-to-textarea-or-webview-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:25:04","modified_gmt":"2022-08-30T15:25:04","slug":"fonts-added-at-runtime-in-ios-to-textarea-or-webview-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/fonts-added-at-runtime-in-ios-to-textarea-or-webview-collection-of-common-programming-errors\/","title":{"rendered":"Fonts added at runtime in IOS to textarea or webview?-Collection of common programming errors"},"content":{"rendered":"<p>Answering my own question.<\/p>\n<p>I have not found a way to do this with a native textarea in iOS without registering the font in the plist file.<\/p>\n<p>How ever, the UIWebView does support @font-face declarations and .ttf files. The trick is to create a NSURL that points to the font stored in the Documents folder and then tell the web view to use this by using [webView stringByEvaluatingJavaScriptFromString].<\/p>\n<p>In my first tests I did something like this:<\/p>\n<p>My view controller:<\/p>\n<pre><code>- (NSString *)documentsDirectory {\n    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,\n                                                         NSUserDomainMask, YES);\n    return [paths objectAtIndex:0];\n}\n\n- (void) setFont {\n    NSString *urlPath = [[self documentsDirectory]\n                         stringByAppendingPathComponent:@\"myfont.ttf\"];\n    NSURL *url = [NSURL fileURLWithPath:urlPath];\n    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@\"setFont('%@');\", url]];\n}\n<\/code><\/pre>\n<p>In my web page:<\/p>\n<pre><code>       \n    body {\n        font-family: customfont;\n        font-size: 40px;\n    }        \n\n\n    function setFont(font) {\n        var id = 'customfont',\n            head = document.getElementsByTagName(\"head\")[0],\n        style;\n\n        style = document.getElementById(id);\n        if(style) {\n            head.removeChild(style);\n        }\n\n        style = document.createElement(\"style\");\n        style.setAttribute(\"id\", id);\n        style.textContent = \"@font-face { font-family: '\" + id + \"'; src: url('\" + font + \"'); font-weight: normal; font-style: normal; }\"\n        head.appendChild(style);\n    }\n\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2014-01-17 07:08:55. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Answering my own question. I have not found a way to do this with a native textarea in iOS without registering the font in the plist file. How ever, the UIWebView does support @font-face declarations and .ttf files. The trick is to create a NSURL that points to the font stored in the Documents folder [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2462","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2462"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2462\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}