ios - Dynamic height for uiwebview with html data -
as part of applications need display html contents in uiwebview , data conatins <ul>
, <li>
tags using attributed string calculating height of webview whenever data comes listed contents calculate incorrect height stuck issue lat 2 days, when list contain short text data returns less heights , contents misssed when comes ipad issue became worst because of large width of webview.
nsmutableattributedstring *htmlstring1 = [[nsmutableattributedstring alloc] initwithdata:[[self.doctordetail objectforkey:@"description"] datausingencoding:nsutf8stringencoding] options:@{nsdocumenttypedocumentattribute: nshtmltextdocumenttype, nscharacterencodingdocumentattribute:[nsnumber numberwithint:nsutf8stringencoding]} documentattributes:null error:nil]; [htmlstring1 addattributes:attrsdictionary range:nsmakerange(0, htmlstring1.length)]; cgsize discriptionsize = cgsizemake(self.discriptionlabel.frame.size.width, flt_max); cgrect textrect = [htmlstring1 boundingrectwithsize:discriptionsize options:nsstringdrawinguseslinefragmentorigin context:nil]; cgsize expectedlabelsize = cgsizemake(textrect.size.width, textrect.size.height); [self.discriptionlabel setframe:cgrectmake(self.discriptionlabel.frame.origin.x, self.doctorimage.frame.origin.y+self.doctorimage.frame.size.height+5, self.discriptionlabel.frame.size.width, expectedlabelsize.height+10)];
you should this:
- (void)viewdidload { [super viewdidload]; webview.delegate = self; [webview loadhtmlstring:@"<div id='foo' style='background: red'>the quick brown fox jumped on lazy dog.</div>" baseurl:nil]; } - (void)webviewdidfinishload:(uiwebview *)webview { nsstring *output = [webview stringbyevaluatingjavascriptfromstring:@"document.getelementbyid(\"foo\").offsetheight;"]; nslog(@"height: %@", output); }
thx answer provided here: http://stackoverflow.com/a/751326/1816644
Comments
Post a Comment