如果我在UITableViewCell中有一个带有排除路径的TextView,我如何计算给定字符串的单元格高度?
解决方法
我找到了一个我认为可能对其他人有帮助的解决方案.由于它不需要创建新的NSTextContainer,NSLayoutManager和NSTextStorage对象,它们已经被实例化为UITextView的一部分,我怀疑它会更有效率.
要计算使用排除路径和NSAttributedString的UITextView的大小,可以执行以下操作:
// Assuming something like this... UIBezierPath * exclusionPath = [UIBezierPath bezierPathWithRect:someRect]; self.textView.textContainer.exclusionPaths = @[exclusionPath]; NSAttributedString * attributedString = ... self.textView.attributedString = attributedString; ... // Use text container,layout manager,and text storage associated with the text view. NSTextContainer * textContainer = self.textView.textContainer; NSLayoutManager * layoutManager = textContainer.layoutManager; NSTextStorage * textStorage = layoutManager.textStorage; // Limit the width or height. In this case,limiting the width to 280. textContainer.size = CGSizeMake(280.0,FLT_MAX); [textStorage setAttributedString:attributedString]; // Because the layout manager performs layout lazily,on demand,you must force it to lay out the text,even though you don’t need the glyph range returned by this function. [layoutManager glyphRangeForTextContainer:textContainer]; // Ask the layout manager for the height of the rectangle occupied by the laid-out text CGFloat height = [layoutManager usedRectForTextContainer:textContainer].size.height;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。