记得以前UIWebView 计算html文本的时候老是会有误差
所以把这个贴出来,留着备用
原始数据:
把内容放到NSAttributedString里来计算:
NSString *richText=@"<p>lfjkdlsajfdjsalf</p>";
NSAttributedString *attributeString=[[NSAttributedString alloc] initWithData:[richText dataUsingEncoding:NSUnicodeStringEncoding]
options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
documentAttributes:nil
error:nil];
使用 NSString 的 boundingRectWithSize 方法来计算
NSRange range=NSMakeRange(0, attributeString.string.length);
NSDictionary *attribute=[attributeString attributesAtIndex:0 effectiveRange:&range];
CGRect rect=[attributeString.string boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-30, 2000)
options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
attributes:attribute
context:nil];
网友评论