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

查看:2488
本文介绍了在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的在线网站,如下所示: http://www.opinionatedgeek.com / 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: http://www.opinionatedgeek.com/dotnet/tools/base64encode/

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

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