在WKWebView中使用自定义字体

编程入门 行业动态 更新时间:2024-10-26 04:31:57
本文介绍了在WKWebView中使用自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在我的应用程式中使用自订字型。它们被复制到bundle并硬编码到appName-info.plist。 这种字体在整个应用程序和UIWebView中工作得很好。

我加载htmlString [webView loadHTMLString:htmlString baseURL :nil]; 我在webView中使用这个字体与css: fontFamily:fontName

但是当我试图使用WkWebView自定义字体不工作。 WkWebView显示随机默认字体。

我试图加载它的主包路径在基本url和使用font-face在css - WkWebView仍然显示随机字体。 >

任何想法如何使自定义字体在WKWebView中工作?

对我的英语很抱歉

解决方案

因为我不想使用另一个第三方只是为了,因为我建立的html字符串本身,我采取的第一部分使用

css看起来像这样:

@ font-face { font-family:'FONTFAMILY'; src:url(data:font / ttf; base64,FONTBASE64)format('truetype'); }

您可以将FONTFAMILY替换为您需要的系列,FONTBASE64

如果您需要在应用程序中创建base64字符串,可以使用此字符串,只需提供文件名和类型使用它来获取其他文件,所以它更通用,你可以删除ofType参数,并使用@ttf代替):

- (NSString *)getBase64FromFile:(NSString *)fileName ofType:(NSString *)type { NSString * filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:type]; //创建NSData对象 NSData * nsdata = [NSData dataWithContentsOfFile:filePath]; //从Base64中的NSData对象获取NSString NSString * base64Encoded = [nsdata base64EncodedStringWithOptions:0]; return base64Encoded; }

如果您只想执行一次,然后将其保存在某些文件,您可以使用任何将文件转换为base64的在线网站,如下所示: www.opinionatedgeek / dotnet / tools / base64encode /

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.

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

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

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

Any ideas how to make custom fonts work in WKWebView?

Sorry for my english

解决方案

Since I don't want to use another third party just for that and since I'm building the html string itself, I took the first part of using the font-face and instead of using a url to a remote or local file, i converted the fonts to base64.

The css looks like this:

@font-face { font-family: 'FONTFAMILY'; src: url(data:font/ttf;base64,FONTBASE64) format('truetype'); }

You can replace the FONTFAMILY with the family that you need and the FONTBASE64 with the base 64 string that was generated from the font.

If you need to create the base64 string in your application, you can use this, just provide the filename and type (i used it to get other files as well so it's more generic, you can remove the ofType parameter and use @"ttf" instead):

- (NSString*)getBase64FromFile:(NSString*)fileName ofType:(NSString*)type { NSString * filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:type]; // Create NSData object NSData *nsdata = [NSData dataWithContentsOfFile:filePath]; // Get NSString from NSData object in Base64 NSString *base64Encoded = [nsdata base64EncodedStringWithOptions:0]; return base64Encoded; }

if you want to do it only one time and then save it in some file, you can use any of the online websites that converts files to base64, like this: www.opinionatedgeek/dotnet/tools/base64encode/

更多推荐

在WKWebView中使用自定义字体

本文发布于:2023-11-07 22:29:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567613.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   字体   WKWebView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!