Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate height of html string before loading into webview in ios 7?

Tags:

ios

iphone

ios7

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

Thanks in Advance

like image 306
mahesh Avatar asked Jun 09 '14 06:06

mahesh


People also ask

How do you calculate height in HTML?

To get the height or width of an element in pixels (including padding and borders), use the offsetHeight and offsetWidth properties of the object.


1 Answers

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.

like image 161
Steve Wilford Avatar answered Sep 23 '22 14:09

Steve Wilford