Objective c 从HTML计算NSAttributedString的大小

Objective c 从HTML计算NSAttributedString的大小,objective-c,uitextview,Objective C,Uitextview,我使用UITextView显示某个给定HTML中的NSAttributed字符串,该字符串可以包括粗体、斜体、列表、标记、超级和下标等元素 目前,下面的代码仅适用于文本的段落,但一旦我开始添加更复杂的元素,如列表和换行符,大小调整就完全停止了 // Create the NSMutableAttributedString from given HTML NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; NSDictio

我使用UITextView显示某个给定HTML中的NSAttributed字符串,该字符串可以包括粗体、斜体、列表、标记、超级和下标等元素

目前,下面的代码仅适用于文本的段落,但一旦我开始添加更复杂的元素,如列表和换行符,大小调整就完全停止了

// Create the NSMutableAttributedString from given HTML
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                          NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithData:data options:options
                               documentAttributes:nil error:nil];

// Max size for the text container (no limit on height)
CGSize bounds = CGSizeMake(320.0, CGFLOAT_MAX);

// Set the font and size for better rendering results
UIFont *font = [UIFont fontWithName:@"Roboto" size:14.0];
NSDictionary *attrFont = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSRange range = NSMakeRange(0, str.length);
[str addAttributes:attrFont range:range];

// Calcualte the size of the UITextView based on the above parameters
CGRect rect = [str boundingRectWithSize:bounds options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|        NSStringDrawingUsesDeviceMetrics context:nil];
我已经做了一些搜索并找到了这条线索,但在尝试了那里的建议后,它似乎仍然不起作用,不知道是否有人知道更好的方法来做到这一点


好的,在反复修改之后,我发现大小实际上是正确的,但是
UITextView
有一些填充/插入导致溢出。在textView上设置以下选项修复了该问题

[self.textView setTextContainerInset:UIEdgeInsetsZero];
self.textView.textContainer.lineFragmentPadding = 0;

“大小完全关闭”和“由于小文本视图插入而关闭”之间有很大区别。顺便说一句-另一种解决方案是通过文本视图的插入调整文本的计算大小。@rmaddy乍一看它是完全关闭的(删除整行),但我可以更新措辞。@rmaddy如何使用插入进行计算?将文本视图的插入添加到计算的
rect