对此的一个解决方法是将 HTML 加载到NSAttributedString并获取其高度。
NSAttributedString *text = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding)}
documentAttributes:nil
error:nil]; // make sure to check for error in a real app
// Then get the bounding rect based on a known width
CGRect textFrame = [text boundingRectWithSize:CGSizeMake(someKnownWidth, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
context:nil];
CGFloat height = textFrame.size.height;
Run Code Online (Sandbox Code Playgroud)
我还没有尝试过使用不同的 HTML,所以 YMMV。
| 归档时间: |
|
| 查看次数: |
1587 次 |
| 最近记录: |