在 WKWebView 中使用自定义字体 [英] Using custom fonts in WKWebView

查看:28
本文介绍了在 WKWebView 中使用自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用自定义字体.它们被复制到包并硬编码到 appName-info.plist.这种字体在整个应用程序和 UIWebView 中都能完美运行.

I'm using custom fonts in my app. They are copied to bundle and hardcoded to appName-info.plist. This fonts works perfectly in the whole app and in UIWebView.

我正在加载 htmlString[webView loadHTMLString:htmlString baseURL:nil];我在带有 css 的 webView 中使用这种字体:fontFamily:字体名称

Im loading htmlString [webView loadHTMLString:htmlString baseURL:nil]; I use this fonts in webView with css: fontFamily: fontName

但是当我尝试使用 WkWebView 自定义字体时,它不起作用.WkWebView 显示随机的默认字体.

But when i try to use WkWebView custom fonts not working. WkWebView displays random default fonts.

我尝试在基本 url 中使用主包路径加载它并在 css 中使用 font-face - WkWebView 仍然显示随机字体.

I tried to load it with main bundle path in base url and using font-face in css - WkWebView still displays random fonts.

如何在 WKWebView 中使用自定义字体?

How can I make custom fonts work in WKWebView?

推荐答案

我遇到了同样的问题,就我而言,我可以使用 base64 编码WITHOUT 修复它>GCDWebServer.

I faced same issue, in my case i could fix it WITHOUT using base64 encoding and GCDWebServer.

场景:

  • WkWebView 加载是通过字符串 html
  • WkWebView 正在使用本地 .css
  • 字体是本地的,并在顶级项目中添加
  • 字体条目在 appName-info.plist 中的键 Fonts provided by application
  • 下提供

解决方案:

在.css顶层添加字体如下

Add font face in .css at top level as follows

@font-face
{
    font-family: 'FontFamily';
    src: local('FontFamily'),url('FontFileName.otf') format('opentype');
}

<小时>

演示项目:

GitHub 项目链接

注意:新的演示应用程序运行可能需要 2-3 秒,我已经测试了它的长 html 字符串,它的工作原理与 UIWebView 相同,没有延迟.相同的字体在 WKWebView 中可能看起来比 UIWebView 小一些.

NOTE: Fresh demo app run may take 2-3 sec, I have tested it for long html string it works same as UIWebView, no lags. Same font may look bit smaller in WKWebView than UIWebView.

这篇关于在 WKWebView 中使用自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆