I want to find the height of html string that is coming from webservice. Kindly suggest me with the best solution.
Thanks in Advance
To get the height or width of an element in pixels (including padding and borders), use the offsetHeight and offsetWidth properties of the object.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With