//网页
UIWebView*_webView=[[UIWebView alloc]initWithFrame:CGRectMake(0,10, screenWidth, 10)];
_webView.delegate = self;
_webView.scrollView.scrollEnabled = NO;
_webView.backgroundColor=[UIColor whiteColor];
if (![PostDic isKindOfClass:[NSNull class]]&&![PostDic[@"content"] isKindOfClass:[NSNull class]]&&PostDic[@"content"]!=nil){
NSString *myStr = [NSString stringWithFormat:@"<head><style>img{max-width:%f !important;}</style></head>",screenWidth-15];
NSString *str = [NSString stringWithFormat:@"%@%@",myStr,@"网页内容"]; [_webView loadHTMLString:str baseURL:nil];
//网页加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *htmlHeight = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"];
NSString *htmlWidth = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth"];
float i = [htmlWidth floatValue]/[htmlHeight floatValue];
int height = (screenWidth)/i;
_webView.frame = CGRectMake(0, _titleTextF.bottom+10, screenWidth, height);
}
本文介绍如何在iOS应用中使用UIWebView展示HTML内容,并实现根据屏幕宽度自适应调整WebView高度的方法。文中详细展示了如何设置UIWebView的属性,加载HTML字符串,以及通过JavaScript获取文档的高度和宽度来动态调整WebView大小。
510

被折叠的 条评论
为什么被折叠?



