我使用以下代码根据html内容查找UIWebview高度,但它没有返回完全正确的大小。有一段时间它会给出更大的尺寸,有时它会给出更小的尺寸。我不想在uiwebview中显示一些额外的空间
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *desccontentH = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"desc\").offsetHeight;"];
NSLog(@"Webviewheight: %@", desccontentH);
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
NSLog(@"sizeThatFits:Height%f, width:%f",fittingSize.width, fittingSize.height);
.....
}
如何根据html内容找到正确的uiwebview高度?
答案 0 :(得分:0)
使用contentSize
的{{1}}的{{1}}属性。
webView
答案 1 :(得分:0)
将此示例用于TextView:
CGFloat descriptionContentHeight = [self sizeOfText:[NSString stringWithFormat:@"%@\n",addressTextView.text] widthOfTextView:addressTextView.frame.size.width withFont:[UIFont fontWithName:@"OpenSans-Light" size:17.5f]].height + 4;
addressTextView.contentInset = UIEdgeInsetsMake(-10,8,0,0);
[addressTextView setFrame:CGRectMake(addressTextView.frame.origin.x,addressTextView.frame.origin.y ,addressTextView.frame.size.width, descriptionContentHeight)];
-(CGSize)sizeOfText:(NSString *)textToMesure widthOfTextView:(CGFloat)width withFont:(UIFont*)font
{
CGSize size = [textToMesure sizeWithFont:font constrainedToSize:CGSizeMake(width-20.0, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
return size;
/// FLT_MAX is definded elsewhere :)
}