【问题标题】:How to change font in webview for loadRequest如何在 webview 中更改 loadRequest 的字体
【发布时间】:2017-08-16 04:17:33
【问题描述】:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString *string = [NSString stringWithFormat:@"document.body.style.zoom = %@;", [UIFont fontWithName:centuryGothicBold size:5]];
    [self.longNewsWebView stringByEvaluatingJavaScriptFromString:string];
}

【问题讨论】:

    标签: objective-c fonts uiwebview ios8


    【解决方案1】:

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

    - (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 是字体大小,也声明为整数常量

    【讨论】:

      猜你喜欢
      • 2018-12-20
      • 2021-06-09
      • 1970-01-01
      • 2012-01-21
      • 2018-03-09
      • 1970-01-01
      • 2013-01-07
      • 2020-03-06
      • 2014-01-12
      相关资源
      最近更新 更多