1

I try to solve the issue with the NSAttributedString and the html in it. I try to draw it in the UILabel as the attributed text in the UITableViewCell.

I have a html string:

<span class=\"\">Безвозмездно</span>

So I made an attributed string with the method:

+ (NSAttributedString *)attributedStringWithHTML:(NSString *)HTML {
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType };
    return [[NSAttributedString alloc] initWithData:[HTML dataUsingEncoding:NSUnicodeStringEncoding] options:options documentAttributes:NULL error:NULL];
}

and then I try to calculate the string height in the class method to get the cell height. I use:

if (attrString.length > 0) {
    height += truncf([attrString boundingRectWithSize:CGSizeMake(300, CGFLOAT_MAX)
                                              options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                              context:nil].size.height) + 1.0f;
}

So I get the height of the label that is smaller than the label which I get with:

[self.titleLabel sizeToFit];

Can anyone help me with this issue?

ShurupuS
  • 2,923
  • 2
  • 24
  • 44
  • 2
    Is it possible that you need to specify the font and its size to get the correct size for the bounding rect? – Shingoo Jun 10 '14 at 15:21
  • All I have the html string like in post above - how can I change the font and its size? – ShurupuS Jun 10 '14 at 15:25
  • Take a look at this post: http://stackoverflow.com/questions/12694137/setting-font-on-nsattributedstring-on-uitextview-disregards-line-spacing – Shingoo Jun 10 '14 at 15:28

1 Answers1

0

I have met this question as well as. And at present I find it appears in UILabel and UITextView. In my opinion it's a bug of Apple. Thus, in order to guarantee the accuracy of result, I prefer to get the height with : [self.titleLabel sizeToFit]

Elijah_Lam
  • 306
  • 3
  • 16