gpt4 book ai didi

ios - 如何在 loadRequest 时向 UIWebView 注入(inject)自定义字体?

转载 作者:行者123 更新时间:2023-11-28 22:19:42 27 4
gpt4 key购买 nike

我使用 UIWebView 以这样的缅甸字体显示搜索测试

    NSString *googleText = @"မြန်မာဘာသာ";
NSString *googleLink = [NSString stringWithFormat:@"http://www.google.com/search?hl=my&lr=lang_my&q=%@",
[[googleText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"
]
];

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:googleLink]]];

这是结果 enter image description here

有什么方法可以在从服务器加载内容时将自定义字体注入(inject)到 UIWebView 中?

我没有在我的包中加载 html 文件,所以我不能在 CSS 中使用自定义字体,如 this questionthis one

求助!谢谢!

最佳答案

您可以引用以下工作代码。

NSString *cssString = [[[[@"body { font-family:" stringByAppendingString:centuryGothicRegular] stringByAppendingString:@"; font-size:"] stringByAppendingString:[NSString stringWithFormat:@"%ld",normalFont]] stringByAppendingString:@"}\""] ; //1

NSString *javascriptString = @"var style = document.createElement('style'); style.innerHTML = '%@'; document.head.appendChild(style)"; // 2
NSString *javascriptWithCSSString = [NSString stringWithFormat:javascriptString, cssString]; // 3
NSLog(@"javascriptWithCSSString: \n %@", javascriptWithCSSString);
NSString *loadedContent = [webView stringByEvaluatingJavaScriptFromString:javascriptWithCSSString];
NSLog(@"loaded ocntent: %@", loadedContent);

Font name and size are declare as constant you can change according to your required font

关于ios - 如何在 loadRequest 时向 UIWebView 注入(inject)自定义字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20759188/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com