首页 > 解决方案 > 在 Apple 的 WebKit webView 中加载自定义字体的问题

问题描述

我目前正在通过我的应用程序从 UIWebView 迁移到受支持的 WKWebView。除了一件事 - 字体外,一切都很好。

即使我的 html(我正在加载)根本没有改变,但在使用 UIWebView 的应用程序版本中,自定义字体确实会加载。另一方面,在使用 WKWebView 的应用程序中,所有内容都加载使用默认字体而不是我正在加载的自定义字体。

WebKit 中的此功能是否存在已知问题?

编辑: 更改源 URL 后,字体会加载。但字体粗细不会因变量而异,如您在下面添加的 HTML 代码中所见。

我在 UIWebView 应用程序和 WKWebView 应用程序中都使用了相同的代码:

NSString *strTemplateHTML = [NSString stringWithFormat:@"<html><head>%@<style>%@</style><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"></head><body style=''>%@</body></html>",[self getAdsScript], styles, self.firstPartArticleContent];

[self.articleWebView loadHTMLString:strTemplateHTML baseURL:NSBundle.mainBundle.bundleURL];

的HTML:

body {
margin:0;
padding:0;
font-family:'Heebo';
font-size:%ldpx !important;
direction: rtl;
width: 100%%;
box-sizing: border-box;
}
@font-face {
  font-family: 'Heebo';
  font-style: normal;
  font-display: swap;
  src: url(https://fonts.gstatic.com/s/heebo/v12/NGS6v5_NC0k9P9H0TbFhsqMA6aw.woff2) format('woff2');
  unicode-range: U+0590-05FF, U+20AA, U+25CC, U+FB1D-FB4F;
}
img {
    max-width:100%%;
    max-height: none;
    height:auto;
    width:100%%;
    box-sizing: border-box;
}
iframe {
    max-width: 100%%;
    max-height: none;
    width: 100%%;
    box-sizing: border-box;
}
a{
    color: #5da1c9;
    white-space: pre-wrap;
    word-wrap: break-word;
}
.OUTBRAIN a {
    white-space: initial; 
} 
.articlelink {
    color: #32AE7B;
    font-size:%ldpx;
    font-family:'Heebo';
    font-weight:700;
}
.streamerPositive {
    color:#34eb7a;
    font-family:'Heebo';
    font-weight:500;
}
.streamerNeutral {
    color:#9FA6A1;
    font-family:'Heebo';
    font-weight:500;
}
.streamerNegative {
    color:#d41717;
    font-family:'Heebo';
    font-weight:500;
};

标签: htmliosswiftobjective-cwebkit

解决方案


推荐阅读