在 WKWebView 中使用自定义本地存储字体
Use custom local stored font with WKWebView
我使用的是 Xcode
和 Swift
的最新版本。
我通过将名为 Blastimo.ttf
的字体拖放到 Xcode 左侧的文件列表中将其导入到我的 Xcode 项目中。
我启用了Target Membership
。
我添加了 Fonts provided by application
> Item 0
: Blastimo.ttf
到 Info.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)
我使用的是 Xcode
和 Swift
的最新版本。
我通过将名为 Blastimo.ttf
的字体拖放到 Xcode 左侧的文件列表中将其导入到我的 Xcode 项目中。
我启用了Target Membership
。
我添加了 Fonts provided by application
> Item 0
: Blastimo.ttf
到 Info.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)