【问题标题】:iOS UIWebView totally fails to understand more than one @font-face?iOS UIWebView 完全无法理解多个@font-face?
【发布时间】: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


【解决方案1】:

<style> 块中有一个错误:它以 <meta> 元素开头。这将使解析器关闭;显然,它认为直到第一个 } 之前的所有内容都是错误的,需要丢弃。

解决方案:将<meta><style> 中取出。

【讨论】:

  • 只是想知道,我真的可以结束元数据吗? <meta id=...></meta>(当然,将其移出风格 - 在风格之前。)
  • 元不需要关闭;他们是void elements,没有结束标签或内容。
  • 在 XHTML 中,您可以在末尾写 />,而不是 >,但不能写 ></meta>
猜你喜欢
  • 1970-01-01
  • 2016-02-02
  • 2016-02-07
  • 2017-04-02
  • 1970-01-01
  • 2017-12-20
  • 2012-02-04
  • 2013-04-19
  • 2016-12-24
相关资源
最近更新 更多