作为我的应用程序的一部分,我需要在uiwebview中显示html内容以及数据conatins <ul>
和<li>
标签我使用属性字符串来计算webview的高度但是每当数据出现时这个列出的内容它会计算出不正确的高度我在这个问题上停留了2天,当列表包含短文本数据时它将返回更少的高度,我的内容将错过,当谈到ipad时问题因为大而变得最糟糕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)];
答案 0 :(得分:0)
你应该做这样的事情:
- (void)viewDidLoad
{
[super viewDidLoad];
webview.delegate = self;
[webview loadHTMLString:@"<div id='foo' style='background: red'>The quick brown fox jumped over the lazy dog.</div>" baseURL:nil];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *output = [webview stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").offsetHeight;"];
NSLog(@"height: %@", output);
}
答案 1 :(得分:0)
我得到了诀窍,
在将<li>
设置为属性字符串
<br>
替换为 valueString = [valueString stringByReplacingOccurrencesOfString:@"<li>" withString:@"<br>"];
[myTable selectRowAtIndexPath: ??????
animated:YES
scrollPosition:UITableViewScrollPositionMiddle];