7

I want to find the height of html string that is coming from webservice. Kindly suggest me with the best solution.

Thanks in Advance

mahesh
  • 97
  • 3
  • http://stackoverflow.com/questions/3936041/how-to-determine-the-content-size-of-a-uiwebview is pretty much what you need, I think? I haven't closed as a duplicate because you're asking about using just the HTML, which I don't think is possible. – jrturton Jun 09 '14 at 06:48
  • 3
    I want to calculate the height of html string without loadint it into webview. – mahesh Jun 09 '14 at 07:09
  • 2
    Then I think you're out of luck. There are so many things that would affect the rendering and sizing that you're unlikely to be able to work this out without actually rendering and measuring – jrturton Jun 09 '14 at 08:12
  • There are so many things affect the hight. Unless you want to know the hight _without_ css and javascript, otherwise I don't think it is possible (without reimplement a layout engine) – Bryan Chen Sep 18 '15 at 05:59

4 Answers4

4

A bit of a workaround to this would be to load the HTML into an NSAttributedString and get it's height.

NSAttributedString *text = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]
                                                            options:@{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                                                                      NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding)}
                                                 documentAttributes:nil
                                                              error:nil]; // make sure to check for error in a real app

// Then get the bounding rect based on a known width
CGRect textFrame = [text boundingRectWithSize:CGSizeMake(someKnownWidth, CGFLOAT_MAX)
                                      options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                      context:nil];

CGFloat height = textFrame.size.height;

I haven't tried this with varied HTML so YMMV.

Steve Wilford
  • 8,894
  • 5
  • 42
  • 66
1
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    float contentSizeForWebview = webView.scrollView.contentSize.height;
}
pkc456
  • 8,350
  • 38
  • 53
  • 109
1

You can calculat hight using following funtion:

-(CGSize)GetTextHightForLable:(NSString*)strText
{
    UILabel *gettingSizeLabel = [[UILabel alloc] init];
    gettingSizeLabel.font = [UIFont fontWithName:@"GillSans-Light" size:13.0f]; // set same fond and size that used in your html string
    gettingSizeLabel.text = strText;
    gettingSizeLabel.numberOfLines = 0;
    CGSize maximumLabelSize = CGSizeMake(270, MAXFLOAT); // 270 is a width you must set same width of you webview this width will be as per your requirement
    CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];
    return expectedSize;
}

So you can get hegith by:

 CGSize HTMLTextHeight =[self GetTextHightForLable:yourString];
 CGFloat height = HTMLTextHeight.size.height;
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
0

I had to deal with web view height calculation issue before. What I can tell here is: it's impossible to calculate the height of a HTML string without loading it to an web view and see its actual height after being rendered. There is another thing you may need to think about is the possibility of having Javascript or hidden elements inside the HTML content.

Ducky
  • 2,754
  • 16
  • 25