在WKWebView中使用自定义本地存储字体



我使用的是最新版本的XcodeSwift

我通过将一个名为Blastimo.ttf的字体拖放到 Xcode 左侧的文件列表中,将其导入到我的 Xcode 项目中。

我启用了Target Membership.

我添加了Fonts provided by application>Item 0Blastimo.ttfInfo.plist

现在我可以将这种字体用于例如标签。这是有效的。

我还想在我的WKWebView中使用这种字体,如下所示:

let html = """
<html>
<body>
<head>
<style>
@font-face
{
font-family: 'Blastimo';
src: local('Blastimo'), url('Blastimo.ttf') format('truetype');
}
</style>
</head>
<h1 style="font-family:'Blastimo';font-size:50px;">This is my test!</h1>
</body>
</html>
"""
webView.loadHTMLString(html, baseURL: nil)

但这不起作用,标准字体被加载。

html代码本身正在使用字体Blastimo.ttf。我在我的网络服务器上测试了它。

但它在WKWebView不起作用.我做错了什么?

解决方案是替换:

webView.loadHTMLString(html, baseURL: nil)

webView.loadHTMLString(html, baseURL: Bundle.main.resourceURL)

最新更新