IOS开发之改变UIWebView文本字体的样式和大小
2016-03-31 20:22
429 查看
1、UIWebView设置字体大小,颜色,字体:1、UIWebView设置字体大小,颜色,字体:
UIWebView无法通过自身的属性设置字体的一些属性,只能通过html代码进行设置,代码如下:UIWebView无法通过自身的属性设置字体的一些属性,只能通过html代码进行设置,代码如下:
[html] view
plaincopy
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];
[html] view
plaincopy
<span style="font-size:14px;">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];</span>
另外也可以通过加载本地的css文件,将格式在css文件中定义,代码如下:
[cpp] view
plaincopy
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];
[cpp] view
plaincopy
<span style="font-size:14px;">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];</span>
2、计算UIWebView的高度
网上有文章介绍使用如下代码计算高度,但用过后感觉不准确,主要是因为有时候html还没加载完计算就不准确了。
[cpp] view
plaincopy
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
[cpp] view
plaincopy
<span style="font-size:14px;">CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];</span>
后来使用如下方法比较准确,就是加载完成后再去计算,不过有个问题是,第一次计算出来高度同时设置了对应高度后,后面计算的结果会受第一次的结果影响,也就是说高度保持第一次计算的结果。我测试的是第一次是内容较多,后面几次是内容较少,但是高度始终保持第一次的高度,不知如果后面的内容比第一次的多,会不会重新计算还是保持第一次的计算结果。所以保险一点,在每次计算之前,先重设一下高度。
[cpp] view
plaincopy
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
const CGFloat defaultWebViewHeight = 22.0;
//reset webview size
CGRect originalFrame = webView.frame;
webView.frame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y, 320, defaultWebViewHeight);
CGSize actualSize = [webView sizeThatFits:CGSizeZero];
if (actualSize.height <= defaultWebViewHeight) {
actualSize.height = defaultWebViewHeight;
}
CGRect webViewFrame = webView.frame;
webViewFrame.size.height = actualSize.height;
webView.frame = webViewFrame;
}
UIWebView无法通过自身的属性设置字体的一些属性,只能通过html代码进行设置,代码如下:UIWebView无法通过自身的属性设置字体的一些属性,只能通过html代码进行设置,代码如下:
[html] view
plaincopy
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];
[html] view
plaincopy
<span style="font-size:14px;">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];</span>
另外也可以通过加载本地的css文件,将格式在css文件中定义,代码如下:
[cpp] view
plaincopy
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];
[cpp] view
plaincopy
<span style="font-size:14px;">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];</span>
2、计算UIWebView的高度
网上有文章介绍使用如下代码计算高度,但用过后感觉不准确,主要是因为有时候html还没加载完计算就不准确了。
[cpp] view
plaincopy
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
[cpp] view
plaincopy
<span style="font-size:14px;">CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];</span>
后来使用如下方法比较准确,就是加载完成后再去计算,不过有个问题是,第一次计算出来高度同时设置了对应高度后,后面计算的结果会受第一次的结果影响,也就是说高度保持第一次计算的结果。我测试的是第一次是内容较多,后面几次是内容较少,但是高度始终保持第一次的高度,不知如果后面的内容比第一次的多,会不会重新计算还是保持第一次的计算结果。所以保险一点,在每次计算之前,先重设一下高度。
[cpp] view
plaincopy
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
const CGFloat defaultWebViewHeight = 22.0;
//reset webview size
CGRect originalFrame = webView.frame;
webView.frame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y, 320, defaultWebViewHeight);
CGSize actualSize = [webView sizeThatFits:CGSizeZero];
if (actualSize.height <= defaultWebViewHeight) {
actualSize.height = defaultWebViewHeight;
}
CGRect webViewFrame = webView.frame;
webViewFrame.size.height = actualSize.height;
webView.frame = webViewFrame;
}
相关文章推荐
- Android cardview 去掉圆角和去掉背景
- java错误:无法将Object转换为int类型
- App测试中ios和Android的区别
- 报错You need to use a Theme.AppCompat theme的两种解决办法
- iOS微博项目(一)
- Android之BroadcastReceiver(Android中的广播接收器)
- Android6.0权限管理以及使用权限该注意的地方
- IOS9.0中Alert的创建
- Android AutoLayout全新的适配方式 堪称适配终结者
- java杂记之将object类转换成int类型,
- 专项测试之APP耗电
- Android Studio常用插件
- JSP内置对象——application对象
- iOS开发:多线程技术概述
- iOS开发之 CollectionViewCell 无法加载
- cocos: 无限循环滚动背景
- iOS开发之 [NSNull length]:unrecognized selector sent to instance
- IOS-60-解决:iOS7 APP页面上下有黑色间距,屏幕变小
- [转] iOS中文版资源库,非常全
- iOS开发之 菊花一直显示不会消失