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

17 篇文章 0 订阅
13 篇文章 0 订阅

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];  
NSString *jsString = [NSString stringWithFormat:@"<html> \n"
                "<head> \n"
                "<style type=\"text/css\"> \n"
                "body {font-size: %f; font-family: \"%@\"; color: %@;}\n"
                "</style> \n"
                "</head> \n"
                "<body>%@</body> \n"
                "</html>", fontSize, fontFamily, fontColor, htmlText];
    [_infoTextView loadHTMLString:jsString baseURL:nil];

另外也可以通过加载本地的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];  
NSString *jsString = [NSString stringWithFormat:@"<html> \n"
    "<head> \n"
    "<link href=\"text.css\" rel=\"stylesheet\" type=\"text/css\"> \n"
    "</style> \n"
    "</head> \n"
    "<body>%@</body> \n"
    "</html>", tempText];
    
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:path];
    [infoWebView loadHTMLString:jsString baseURL:baseURL];

2、计算UIWebView的高度

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

  1. CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];  
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];

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

  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
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值