【问题标题】:WKWebView doesn't show fonts installed by font provider appsWKWebView 不显示字体提供程序应用程序安装的字体
【发布时间】:2020-12-15 13:45:58
【问题描述】:

从 iOS 13 开始,应用可以使用新的Fonts 授权,并带有一个子选项“Use Installed Fonts”来使用由特殊字体提供商安装的自定义字体 应用程序。这些应用程序使用相同的权利,但带有子选项“Install Fonts”。此类应用的示例包括 Adob​​e 的 CreativeCloud 和 MonoType 的 MyFonts

使用安装的字体可以正常工作,WKWebView 除外。

例如,我使用以下代码在WKWebView 控件中加载一些文本,并使用UIFontPickerViewController 选择字体。选择字体后,webView 控件和导航栏的字体都在更新。这适用于内置系统字体。但是在选择自定义安装的字体后,只有导航栏的字体正在更新。这表示应用支持安装的自定义字体,WKWebView 控件除外。

class ViewController: UIViewController {

    @IBOutlet weak var webView: WKWebView!

    var font = "Avenir Next"

    let html = """
    <style>
    body {
        font-family: 'FONTFAMILY';
        font-size: 40pt;
    }
    </style>

    <body>
    This is a test string to demonstrate the characters of a custom installed font named “<b>FONTFAMILY</b>”. Hopefully it will not be replaced by Times New Roman or some other ugly looking font.
    </body>
    """

    override func viewDidLoad() {
        super.viewDidLoad()
        loadHTML()
    }

    @IBAction func selectFont(_ sender: AnyObject) {
        let picker = UIFontPickerViewController()
        picker.delegate = self
        present(picker, animated: true, completion: nil)
    }

    fileprivate func loadHTML() {
        webView.loadHTMLString(html.replacingOccurrences(of: "FONTFAMILY", with: font), baseURL: nil)
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font : UIFont(name: font, size: 24)!]
    }
}

extension ViewController: UIFontPickerViewControllerDelegate {
    func fontPickerViewControllerDidPickFont(_ viewController: UIFontPickerViewController) {
        if let family = viewController.selectedFontDescriptor?.object(forKey: .family) as? String      {
            font = family
            loadHTML()
        }
    }
}

问题:我必须怎么做才能让这些自定义安装的字体在WKWebView 中工作?

stackoverflow 中关于 WKWebView 和外部字体的大多数现有问题都涉及从其捆绑包或外部服务器加载自定义字体的应用程序。在 iOS 中安装自定义字体的旧技术使用了配置文件。这与 WKWebView 完美配合,但我的问题是关于新的 iOS 13 字体安装技术,如下所述:https://developer.apple.com/videos/play/wwdc2019/227/

【问题讨论】:

    标签: ios swift uikit wkwebview uifontpickerviewcontroller


    【解决方案1】:

    在通过 DTS 联系 Apple 后,我收到了一个间接确认,即此问题是一个错误,因为“它可能需要更改您正在使用的操作系统、工具或框架”。我已提交反馈,希望一切顺利。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      相关资源
      最近更新 更多