【发布时间】:2016-07-04 16:14:56
【问题描述】:
注意这个简单的 css/html 正在本地 UIWebView 中显示:
有模拟器显示...
注意有两个@font-face 定义。
但是...... 只有第二个有效。如果你交换它们,只有第二个有效。
所以这里...
@font-face {
font-family:'aaa';
src: local('SourceSansPro-Regular'),
url('SourceSansPro-Regular.ttf') format('truetype');
}
@font-face {
font-family:'bbb';
src: local('SourceSansPro-BoldIt'),
url('SourceSansPro-BoldItalic.ttf') format('truetype');
}
只有“bbb”有效,另一个似乎已“取消”。那么这里..
@font-face {
font-family:'bbb';
src: local('SourceSansPro-BoldIt'),
url('SourceSansPro-BoldItalic.ttf') format('truetype');
}
@font-face {
font-family:'aaa';
src: local('SourceSansPro-Regular'),
url('SourceSansPro-Regular.ttf') format('truetype');
}
只有“aaa”有效,另一个似乎已“取消”。
这是在 Swift 中的操作方法,
// load a simple UIWebView (say, an "about" or "legal" screen)
// base is in template.html
import UIKit
import WebKit
class MinorWebView:UIViewController
{
@IBOutlet var wv:UIWebView!
@IBInspectable var filename:String = "?"
// for example, "about" for "about.html"
...
func makeHtml()
{
print("making html for ... " ,filename)
let ourSize = grid.yourSizeDecisionMechanism
let sizeString = String(format:"%.0f", ourSize)
let p1 = NSBundle.mainBundle().pathForResource("template", ofType: "html")
var html:String = try! NSString(contentsOfFile:p1!, encoding:NSUTF8StringEncoding) as String
let p2 = NSBundle.mainBundle().pathForResource(filename, ofType: "html")
let content:String = try! NSString(contentsOfFile:p2!, encoding:NSUTF8StringEncoding) as String
html = html.stringByReplacingOccurrencesOfString("STUFF", withString:content)
html = html.stringByReplacingOccurrencesOfString("SIZEPOINTS", withString:sizeString)
print("Made html like this ---\n" ,html ,"\n------")
wv.loadHTMLString(html as String, baseURL:nil)
}
}
只需将“template.html”和“about.html”拖到项目中(就像使用图像一样);确保他们有目标成员。
【问题讨论】:
-
你试过WKWebView吗?
-
感谢阿尔贝托的提问;它似乎在 WK 中完全失败了。 (仅使用默认类型。)在这里,我真的想让它在 UIWebView 中工作,谢谢。
标签: ios css uiwebview font-face custom-font