分享

ios开发之改变UIWebView文本字体的样式和大小

 求知665 2014-12-30

1、UIWebView设置字体大小,颜色,字体:1、UIWebView设置字体大小,颜色,字体:

UIWebView无法通过自身的属性设置字体的一些属性,只能通过html代码进行设置,代码如下:UIWebView无法通过自身的属性设置字体的一些属性,只能通过html代码进行设置,代码如下:

  1. NSString *jsString = [NSString stringWithFormat:@"<html> \n"  
  2.                 "<head> \n"  
  3.                 "<style type=\"text/css\"> \n"  
  4.                 "body {font-size: %f; font-family: \"%@\"; color: %@;}\n"  
  5.                 "</style> \n"  
  6.                 "</head> \n"  
  7.                 "<body>%@</body> \n"  
  8.                 "</html>", fontSize, fontFamily, fontColor, htmlText];  
  9.     [_infoTextView loadHTMLString:jsString baseURL:nil];  
  1. <span style="font-size:14px;">NSString *jsString = [NSString stringWithFormat:@"<html> \n"  
  2.                 "<head> \n"  
  3.                 "<style type=\"text/css\"> \n"  
  4.                 "body {font-size: %f; font-family: \"%@\"; color: %@;}\n"  
  5.                 "</style> \n"  
  6.                 "</head> \n"  
  7.                 "<body>%@</body> \n"  
  8.                 "</html>", fontSize, fontFamily, fontColor, htmlText];  
  9.     [_infoTextView loadHTMLString:jsString baseURL:nil];</span>  

另外也可以通过加载本地的css文件,将格式在css文件中定义,代码如下:

  1. NSString *jsString = [NSString stringWithFormat:@"<html> \n"  
  2.     "<head> \n"  
  3.     "<link href=\"text.css\" rel=\"stylesheet\" type=\"text/css\"> \n"  
  4.     "</style> \n"  
  5.     "</head> \n"  
  6.     "<body>%@</body> \n"  
  7.     "</html>", tempText];  
  8.       
  9.     NSString *path = [[NSBundle mainBundle] bundlePath];  
  10.     NSURL *baseURL = [NSURL fileURLWithPath:path];  
  11.     [infoWebView loadHTMLString:jsString baseURL:baseURL];  
  1. <span style="font-size:14px;">NSString *jsString = [NSString stringWithFormat:@"<html> \n"  
  2.     "<head> \n"  
  3.     "<link href=\"text.css\" rel=\"stylesheet\" type=\"text/css\"> \n"  
  4.     "</style> \n"  
  5.     "</head> \n"  
  6.     "<body>%@</body> \n"  
  7.     "</html>", tempText];  
  8.       
  9.     NSString *path = [[NSBundle mainBundle] bundlePath];  
  10.     NSURL *baseURL = [NSURL fileURLWithPath:path];  
  11.     [infoWebView loadHTMLString:jsString baseURL:baseURL];</span>  

2、计算UIWebView的高度

网上有文章介绍使用如下代码计算高度,但用过后感觉不准确,主要是因为有时候html还没加载完计算就不准确了。

  1. CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];  
  1. <span style="font-size:14px;">CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];</span>  

后来使用如下方法比较准确,就是加载完成后再去计算,不过有个问题是,第一次计算出来高度同时设置了对应高度后,后面计算的结果会受第一次的结果影响,也就是说高度保持第一次计算的结果。我测试的是第一次是内容较多,后面几次是内容较少,但是高度始终保持第一次的高度,不知如果后面的内容比第一次的多,会不会重新计算还是保持第一次的计算结果。所以保险一点,在每次计算之前,先重设一下高度。

  1. - (void)webViewDidFinishLoad:(UIWebView *)webView  
  2. {  
  3.     const CGFloat defaultWebViewHeight = 22.0;  
  4.     //reset webview size   
  5.     CGRect originalFrame = webView.frame;  
  6.     webView.frame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y, 320, defaultWebViewHeight);  
  7.       
  8.     CGSize actualSize = [webView sizeThatFits:CGSizeZero];  
  9.     if (actualSize.height <= defaultWebViewHeight) {  
  10.         actualSize.height = defaultWebViewHeight;  
  11.     }  
  12.     CGRect webViewFrame = webView.frame;  
  13.     webViewFrame.size.height = actualSize.height;  
  14.     webView.frame = webViewFrame;  
  15.       
  16. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多