我使用的是最新版本的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)