【问题标题】:Dynamic height for uiwebview with html data带有html数据的uiwebview的动态高度
【发布时间】:2015-06-14 12:21:14
【问题描述】:

作为我的应用程序的一部分,我需要在 uiwebview 和数据 conatins <ul> 和 <li> 标记中显示 html 内容,我使用属性字符串来计算 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)];

【问题讨论】:

    标签: ios uiwebview nsattributedstring


    【解决方案1】:

    你可能应该这样做:

    - (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

    【讨论】:

    • 感谢重播。
    【解决方案2】:

    我知道了,

    刚刚将&lt;li&gt; 替换为&lt;br&gt;,然后将其设置为属性字符串

       valueString = [valueString stringByReplacingOccurrencesOfString:@"<li>" withString:@"<br>"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多