如果html元素是<p>
或<div>
,则rect将是错误的。
如果html元素为<span>
,则rect将是正确的。
例如
http://test-1252137158.file.myqcloud.com/myFloder/WX20180717-112628%402x.png
http://test-1252137158.file.myqcloud.com/myFloder/WX20180717-112649%402x.png
http://test-1252137158.file.myqcloud.com/myFloder/WX20180717-112706%402x.png
代码:
答案 0 :(得分:0)
您还需要传递标记NSStringDrawingUsesFontLeading
来考虑所使用的字体。
类似的东西(第41行):
CGRect rect = [str boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
context:NULL];
答案 1 :(得分:0)
NSRange tailRange = NSMakeRange(attributedString.length - 1, 1);
if (attributedString.length > 1 &&
[[[attributedString attributedSubstringFromRange:tailRange] string] isEqualToString:@"\n"]) {
[attributedString replaceCharactersInRange:tailRange withString:@""];
}
CGRect rect = [self.attributeString boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:NULL];
ceilf(rect.size.height);