繁体   English   中英

在 swift 4.2 中加载动态 html 页面时,如何在 iOS 上为 wkWebview 实现本地自定义字体?

[英]How to implement local custom font for wkWebview on iOS when loading dynamic html page in swift 4.2?

首先,我在 web 部件中创建了我的 style.css

@font-face {
  font-family: "Noto Sans Southeast Asian";
  font-weight: normal;
  src: local("Noto Sans Southeast Asian"), url("NotoSansSoutheastAsian-Regular.ttf") format("truetype");
}
@font-face {
  font-family: "Noto Sans Southeast Asian Bold";
  font-weight: bold;
  src: local("Noto Sans Southeast Asian Bold"), url("NotoSansSoutheastAsian-Bold.ttf") format("truetype");
}

我已经完成了本机 iOS 部分自定义字体的此指令无法以编程方式在 swift 中工作

它没有用

我得到的建议是,如果我想动态地将我的 HTML 页面编码为 base64,然后将其带到本机部分以添加 wkWebview 的本地字体

我不知道如何实现 wkWebview 以在我的项目从 Html 文件加载的每个页面中使用我的自定义字体

我刚开始使用 wkWebview。 我的项目使用 swift 4.2

func updateHTMLStringStyle(fontSize: String, bodyContent: String) -> String {

 return "<html><head><meta charset='utf-8'><meta name='viewport' content='width=device-width, initial-scale=1, shrink-to-fit=no'><meta name='theme-color' content='#000000'><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' /><meta name='theme-color' content='#000000'><meta name='msapplication-navbutton-color' content='#5A9C14'><meta name='apple-mobile-web-app-status-bar-style' content='#000000'><style>a:link { color: blue; }</style><link href='https://fonts.googleapis.com/css?family=ProximaNova-Regular' rel='stylesheet'><style>:root,* {font-family: ProximaNova-Regular;font-size: \(fontSize);color: "rgb(216, 216, 216)");text-align: left;-webkit-user-select: none;user-select: none;}</style></head><body>\(bodyContent ?? "")</body></html>"

}       

您可以根据需要自定义此 HTML 格式中的字体样式、文本颜色、字体大小。 在上面的函数中传递 webview 内容和字体大小。

使用功能

webViewBody.loadHTMLString((self.updateHTMLStringStyle(fontSize: "21px"))!, baseURL: nil)

我希望,这会奏效。

对于我的情况,我通过在style.css中的 web 部件上编辑解决了这个问题,并将 WOFF 字体类型上传到我的网站,然后链接到它。

我只知道 iOS Safari 从 iOS 10 开始支持 WOFF 和 WOFF2。*

@font-face {
  font-family: "Noto Sans Southeast Asian";
  src: local("Noto Sans Southeast Asian"), 
       url(LINK_TO_FONT/NotoSansSoutheastAsian-Regular.woff) format("woff");
  font-weight: normal;
  font-style: normal;

}
@font-face {
  font-family: "Noto Sans Southeast Asian Bold";
  src: local("Noto Sans Southeast Asian Bold"), 
       url(LINK_TO_FONT/NotoSansSoutheastAsian-Bold.woff) format("woff");
  font-weight: bold;
  font-style: normal;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM