我正在创建一个 WebView 以在我的应用中显示一些文本。
CGRect frame = CGRectMake(lblContent.frame.origin.x,
lblContent.frame.origin.y,
lblContent.frame.size.width,
lblContent.frame.size.height);
NSString *htmlText = HTML_DIV_TAG;
htmlText = [htmlText stringByAppendingFormat:@"%@%@", _artistDetail.strContent, @"</div>"];
htmlText = [htmlText stringByReplacingOccurrencesOfString:@"''" withString:@"'"];
UIWebView *webView = [[UIWebView alloc]initWithFrame:frame];
[webView loadHTMLString:htmlText baseURL:nil];
webView.scrollView.bounces = NO;
[self addSubview:webView];
这是我放入我的应用程序中的一组代码。
但是需要根据内容大小调整webview的高度。
最佳答案
如果设置 webview.delegate = self
,您可以添加 UIWebViewDelegate
协议(protocol)方法 webViewDidFinishLoad:
并调用一小段 javascript 来确定高度:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *output = [webview stringByEvaluatingJavaScriptFromString:@"document.body. scrollHeight;"];
NSLog(@"height: %@", output);
}
另一种方法是在此方法中确定 webView 的 scrollView contentSize
,因为在调用此方法时它应该适合整个网站。
此方法的唯一问题是当页面 完成加载时调用此方法。图像的处理方式不同,这意味着 webView 可能会因完成加载的图像而改变大小。
关于iphone - 根据内容大小改变UIWebView的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13340191/