I want to find the height of html string that is coming from webservice. Kindly suggest me with the best solution.
Thanks in Advance
I want to find the height of html string that is coming from webservice. Kindly suggest me with the best solution.
Thanks in Advance
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.
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
float contentSizeForWebview = webView.scrollView.contentSize.height;
}
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;
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.