如何在webview中更改loadRequest的字体

时间:2017-03-23 14:27:57

标签: objective-c fonts uiwebview ios8

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString *string = [NSString stringWithFormat:@"document.body.style.zoom = %@;", [UIFont fontWithName:centuryGothicBold size:5]];
    [self.longNewsWebView stringByEvaluatingJavaScriptFromString:string];
}

1 个答案:

答案 0 :(得分:0)

经过长时间的研究后,终于得到了我的问题的答案

- (void)webViewDidFinishLoad:(UIWebView *)webView{
[self stopProgressHUD];
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);

}

  

centuryGothicRegular是声明为常量的字体名称。   normalFont是字体大小,也声明为整数常量