iOS custom font path for UIWebViews

Deadly 提交于 2019-11-30 15:26:42
Pavel Oganesyan

First of all, add filed "Fonts provided by application" in your info.plist. It will be an array like

item0 myFont.otf
item1 anotherFont.ttf

or something.

In HTML use font-face to add a font. And place your font files in the same directory with html file. If HTML is generated dynamically and not shown from bundle then you should copy font .

Good luck.

I got a working html code example here.

<html>
<head>
    <style type='text/css'>
        p.p1 {margin: 0.0px 0.0px 0.0px 30.0px; font: 34.0px;}
        @font-face {
            font-family: 'Nosifer'; font-style: normal; font-weight: 400; src: local('Nosifer'), local('Nosifer-Regular'), url('Nosifer-Regular.ttf') format('truetype');
        }

        .nosifer {
            font-family: Nosifer;
        }

        </style>
</head>
<body allowtransparency='true'>
    <p class="nosifer">Nosifer ABCabc</p>
</body>

As you can see I used a css class to define the font more easily. I'm not sure what all that CSS code within font-face means, but it seems to work.

I confirmed that you NEED to add "Nosifer-Regular.ttf" to the array of fonts provided by the application in the *-info.plist file (key: Fonts provided by application). Btw I googled Nosifer-Regular.ttf and found it on a noisy site here. It really worked though: http://www.ffonts.net/Nosifer.font.download

Then, I was also able to load an OTF that I'll be using in my case. In the CSS I changed format from truetype to opentype.

Add your custom font in your project. Then

Get font name

 UIFont *font = [UIFont fontWithName:@"Your-Custom-font" size:20];

Then create html string

NSString *htmlHeader=[NSString stringWithFormat:@"<html><head><head><link rel=\"stylesheet\" type=\"text/css\" href=\"file://localhost%@\"></head><body><font face=\"%@\"; size=\"4\">HI</font></body></html>",cssPath,font.familyName];

Good luck.

metatation

Here is how you'd add the font to your Info.plist:

    <key>UIAppFonts</key>
    <array>
        <string>Nosifer-Regular.ttf</string>
    </array>

Then use @font-face as per this other answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!