xcode 自定义字体在 WKWebView Swift 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49370238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:35:19  来源:igfitidea点击:

Custom Font Not Working in WKWebView Swift

iosswiftxcodeswift4wkwebview

提问by ihy

Trying to use custom font in WKWebView but no luck.

尝试在 WKWebView 中使用自定义字体但没有运气。

let htmlString = "<span style=\"font-family: 'OpenSans-Bold'; font-size: 30; color: white\">\(Utils.aboutUsText)</span>"
webView.loadHTMLString(htmlString, baseURL: nil)

I can use HelveticaNeue-Bold and works great but not with the custom font above.

我可以使用 HelveticaNeue-Bold 并且效果很好,但不能使用上面的自定义字体。

let htmlString = "<span style=\"font-family: 'HelveticaNeue'; font-size: 30; color: white\">\(Utils.aboutUsText)</span>"
webView.loadHTMLString(htmlString, baseURL: nil)

I have added the custom fonts properly.See screenshots.

我已正确添加自定义字体。请参阅屏幕截图。

Can someone please tell me how can i achieve this or point me in the right direction.

有人可以告诉我如何实现这一目标或为我指明正确的方向。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by OOPer

Reading the answers in the linked threadin DonMag's comment:

阅读DonMag 评论中链接线程中的答案:

  • Using @font-faceis mandatory

  • You need multiple @font-facedeclarations to use multiple font files as a single font family

  • You need to provide baseURLto make relative urls like url(OpenSans-Regular.ttf)work

  • 使用@font-face是强制性的

  • 您需要多个@font-face声明才能将多个字体文件用作单个字体系列

  • 您需要提供baseURL使相关网址像url(OpenSans-Regular.ttf)工作一样

So, try this:

所以,试试这个:

    let htmlString = """
    <style>
    @font-face
    {
        font-family: 'Open Sans';
        font-weight: normal;
        src: url(OpenSans-Regular.ttf);
    }
    @font-face
    {
        font-family: 'Open Sans';
        font-weight: bold;
        src: url(OpenSans-Bold.ttf);
    }
    @font-face
    {
        font-family: 'Open Sans';
        font-weight: 900;
        src: url(OpenSans-ExtraBold.ttf);
    }
    @font-face
    {
        font-family: 'Open Sans';
        font-weight: 200;
        src: url(OpenSans-Light.ttf);
    }
    @font-face
    {
        font-family: 'Open Sans';
        font-weight: 500;
        src: url(OpenSans-Semibold.ttf);
    }
    </style>
    <span style="font-family: 'Open Sans'; font-weight: bold; font-size: 30; color: red">(Utils.aboutUsText)</span>
    """
    webView.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL) //<- 


Or you can use a separate css file if you prefer:

或者,如果您愿意,也可以使用单独的 css 文件:

    let htmlString = """
    <link rel="stylesheet" type="text/css" href="open-sans.css">
    <span style="font-family: 'Open Sans'; font-weight: bold; font-size: 30; color: red">(Utils.aboutUsText)</span>
    """
    webView.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL)

open-sans.css:

打开-sans.css:

@font-face
{
    font-family: 'Open Sans';
    font-weight: normal;
    src: url(OpenSans-Regular.ttf);
}
@font-face
{
    font-family: 'Open Sans';
    font-weight: bold;
    src: url(OpenSans-Bold.ttf);
}
@font-face
{
    font-family: 'Open Sans';
    font-weight: 900;
    src: url(OpenSans-ExtraBold.ttf);
}
@font-face
{
    font-family: 'Open Sans';
    font-weight: 200;
    src: url(OpenSans-Light.ttf);
}
@font-face
{
    font-family: 'Open Sans';
    font-weight: 500;
    src: url(OpenSans-Semibold.ttf);
}