Exact height of NSTextContainer

大兔子大兔子 提交于 2019-12-11 04:08:02

问题


I have a method used to calculate the exact height size for a text container. The method returns not correct values when trying to measure the height of an attributed string filled with arabic html content.

This is the code used :

- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
NSTextStorage *textStorager = [[NSTextStorage alloc] initWithAttributedString:text];
NSTextContainer *textContainerr = [[NSTextContainer alloc] init];
textContainerr.size = CGSizeMake(width, FLT_MAX);
NSLayoutManager *layoutManagerr = [[NSLayoutManager alloc] init];
[layoutManagerr addTextContainer:textContainerr];
[textStorager addLayoutManager:layoutManagerr];
[textStorager addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:self.fontSize] range:NSMakeRange(0, [textStorager length])];
[textContainerr setLineFragmentPadding:5.0];
layoutManagerr.allowsNonContiguousLayout = NO;
CGFloat size = [layoutManagerr usedRectForTextContainer:textContainerr].size.height;
NSLog(@"size: %f - height_TextView:%f",size,height_TextView);

return size;
}

Is there any solution for measuring the exact height of any html content ?

Thanks

来源:https://stackoverflow.com/questions/32613836/exact-height-of-nstextcontainer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!