首先,我在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部分完成了此说明
custom font not working programmatically in 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)


我希望这会起作用。

10-08 07:20
查看更多