![](/img/trans.png)
[英]ios textview attributedText load html font working in device but not simulator
[英]Font works on Simulator but not iOS device
有一些問題已經與這個問題相關,但我的情況不同。 就我而言,字體是從 zip 文件下載並保存在此處:
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
這是 HTML 字符串:
<html>
<head>
<style type=text/css>
@font-face { font-family: 'Rubik-Regular'; src: url('Rubik-Regular.ttf'); }
@font-face { font-family: 'Rubik-Semibold'; src: url('Rubik-Medium.ttf'); }
b { font-size:20px; color:#373534; letter-spacing: 1.27px; line-height: 30px; font-family: 'Rubik-Semibold'; background:transparent;)
}
p { text-align:left; font-size:20px; color:#373534; letter-spacing: 1.27px; line-height: 30px; font-family: 'Rubik-Regular'; background:transparent; }
</style>
</head>
<body>
<header>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0'>
<p>This is the text for the HTML and we <b>BOLD</b> this part</p>
</header>
</body>
</html>
然后我在我的 webView 上調用 loadHTMLString:
let url = URL(fileURLWithPath: path!)
wkWebView.loadHTMLString(htmlString, baseURL: url)
不知道為什么它可以在模擬器和 Android 設備上完美加載,但 HTML 字符串在真實設備上會出現問題。 為什么真機不能從本地存儲加載字體?
我在原始實現中遺漏的是 loadFileURL(_ URL, _ URL) 方法。 這個方法應該在調用 loadHTMLString 之前,對我有用的是在我的自定義視圖類的awakedFromNib 方法中調用loadFileURL()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.