WebVeiw 宽度自适应
来源:互联网 发布:知乎 女生 打男朋友 编辑:程序博客网 时间:2024/06/07 02:47
NSString *urlStr = [NSString stringWithFormat:@"<html><head></head><body>%@</body></html>",detailModel.content];
WebView.scrollView.bounces = NO;
WebView.scalesPageToFit=NO;
[WebView loadHTMLString:urlStr baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",HOST_URL]]];
//html是否加载完成
isLoadingFinished = NO;
//第一次加载先隐藏webview
[WebView setHidden:YES];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if(navigationType==UIWebViewNavigationTypeLinkClicked)//判断是否是点击链接
{
return NO;
}
else{return YES;}
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//若已经加载完成,则显示webView并return
if(isLoadingFinished)
{
[WebView setHidden:NO];
return;
}
//js获取body宽度
NSString *bodyWidth= [webView stringByEvaluatingJavaScriptFromString: @"document.body.scrollWidth "];
int widthOfBody = [bodyWidth intValue];
//获取实际要显示的html
NSString *html = [self htmlAdjustWithPageWidth:widthOfBody
html:[NSString stringWithFormat:@"<html><head></head><body>%@</body></html>",detailModel.content]
webView:webView];
//设置为已经加载完成
isLoadingFinished = YES;
//加载实际要现实的html
[WebView loadHTMLString:html baseURL:nil];
/
}
//获取宽度已经适配于webView的html。这里的原始html也可以通过js从webView里获取
- (NSString *)htmlAdjustWithPageWidth:(CGFloat )pageWidth
html:(NSString *)html
webView:(UIWebView *)webView
{
NSMutableString *str = [NSMutableString stringWithString:html];
//计算要缩放的比例
CGFloat initialScale = webView.frame.size.width/pageWidth;
//将</head>替换为meta+head
NSString *stringForReplace = [NSString stringWithFormat:@"<meta name=\"viewport\" content=\" initial-scale=%f, minimum-scale=0.1, maximum-scale=2.0, user-scalable=yes\"></head>",initialScale];
NSRange range = NSMakeRange(0, str.length);
//替换
[str replaceOccurrencesOfString:@"</head>" withString:stringForReplace options:NSLiteralSearch range:range];
return str;
WebView.scrollView.bounces = NO;
WebView.scalesPageToFit=NO;
[WebView loadHTMLString:urlStr baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",HOST_URL]]];
//html是否加载完成
isLoadingFinished = NO;
//第一次加载先隐藏webview
[WebView setHidden:YES];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if(navigationType==UIWebViewNavigationTypeLinkClicked)//判断是否是点击链接
{
return NO;
}
else{return YES;}
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//若已经加载完成,则显示webView并return
if(isLoadingFinished)
{
[WebView setHidden:NO];
return;
}
//js获取body宽度
NSString *bodyWidth= [webView stringByEvaluatingJavaScriptFromString: @"document.body.scrollWidth "];
int widthOfBody = [bodyWidth intValue];
//获取实际要显示的html
NSString *html = [self htmlAdjustWithPageWidth:widthOfBody
html:[NSString stringWithFormat:@"<html><head></head><body>%@</body></html>",detailModel.content]
webView:webView];
//设置为已经加载完成
isLoadingFinished = YES;
//加载实际要现实的html
[WebView loadHTMLString:html baseURL:nil];
/
}
//获取宽度已经适配于webView的html。这里的原始html也可以通过js从webView里获取
- (NSString *)htmlAdjustWithPageWidth:(CGFloat )pageWidth
html:(NSString *)html
webView:(UIWebView *)webView
{
NSMutableString *str = [NSMutableString stringWithString:html];
//计算要缩放的比例
CGFloat initialScale = webView.frame.size.width/pageWidth;
//将</head>替换为meta+head
NSString *stringForReplace = [NSString stringWithFormat:@"<meta name=\"viewport\" content=\" initial-scale=%f, minimum-scale=0.1, maximum-scale=2.0, user-scalable=yes\"></head>",initialScale];
NSRange range = NSMakeRange(0, str.length);
//替换
[str replaceOccurrencesOfString:@"</head>" withString:stringForReplace options:NSLiteralSearch range:range];
return str;
}
感谢作者 http://borissun.iteye.com/blog/2023712 0 0
- WebVeiw 宽度自适应
- 宽度自适应
- 自适应宽度
- 自适应布局---自适应宽度
- 图片宽度自适应浏览器宽度
- Vickate_iOS_UILable自适应宽度/自适应高度
- 使TStringGrid自适应宽度
- ifram 自适应框架宽度
- Iframe 自适应高度宽度
- [CSS]自适应宽度按钮
- CListCtrl自适应宽度
- 一列宽度自适应
- 二列右列宽度自适应布局
- 使TStringGrid自适应宽度
- poi 列宽度自适应
- CSS实现自适应宽度
- EXTJS GridPanel自适应宽度
- TStringGrid自适应宽度
- mysql架构解析
- dbms_random.string用法
- 南洋理工ACM 53无聊的小明
- TopCoder SRM 637 Div2 B
- 银行自动存取一体机例题
- WebVeiw 宽度自适应
- LeetCode 316: Remove Duplicate Letters
- ofbiz erp 的整体方案设计
- 利用GDataXML进行XML文件解析
- 51nod 1283 最小周长
- BZOJ 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘
- 第15周实践项目1——程序填空
- 解决Linux连不上外国软件源或者软件源失效
- Codeforces #1 A