Objective c 如何为loadRequest更改webview中的字体
经过长时间的研究,我的问题终于得到了答案Objective c 如何为loadRequest更改webview中的字体,objective-c,fonts,uiwebview,ios8,Objective C,Fonts,Uiwebview,Ios8,经过长时间的研究,我的问题终于得到了答案 - (void)webViewDidFinishLoad:(UIWebView *)webView{ NSString *string = [NSString stringWithFormat:@"document.body.style.zoom = %@;", [UIFont fontWithName:centuryGothicBold size:5]]; [self.longNewsWebView stringByEvaluating
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *string = [NSString stringWithFormat:@"document.body.style.zoom = %@;", [UIFont fontWithName:centuryGothicBold size:5]];
[self.longNewsWebView stringByEvaluatingJavaScriptFromString:string];
}
}
centuryGothicRegular是声明为常量的字体名称。
normalFont是字体大小,也声明为整数常量
- (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);