uiwebview的动态高度与html数据

时间:2015-06-14 12:21:15

标签: ios uiwebview nsattributedstring

作为我的应用程序的一部分,我需要在uiwebview中显示html内容以及数据conatins <ul><li>标签我使用属性字符串来计算webview的高度但是每当数据出现时这个列出的内容它会计算出不正确的高度我在这个问题上停留了2天,当列表包含短文本数据时它将返回更少的高度,我的内容将错过,当谈到ipad时问题因为大而变得最糟糕webview的宽度。

screenshots screenshots

 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)];

2 个答案:

答案 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);
}

这里提供的答案是: http://stackoverflow.com/a/751326/1816644

答案 1 :(得分:0)

我得到了诀窍,

在将<li>设置为属性字符串

之前,将<br>替换为 valueString = [valueString stringByReplacingOccurrencesOfString:@"<li>" withString:@"<br>"];
[myTable selectRowAtIndexPath: ??????
                 animated:YES 
           scrollPosition:UITableViewScrollPositionMiddle];