ios – 使用排除路径计算TextView的单元格高度

ios – 使用排除路径计算TextView的单元格高度,第1张

概述如果我在UITableViewCell中有一个带有排除路径的TextView,我如何计算给定字符串的单元格高度? 我找到了一个我认为可能对其他人有帮助的解决方案.由于它不需要创建新的NSTextContainer,NSLayoutManager和NSTextStorage对象,它们已经被实例化为UITextView的一部分,我怀疑它会更有效率. 要计算使用排除路径和NSAttributedStri 如果我在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 textCGfloat height = [layoutManager usedRectForTextContainer:textContainer].size.height;

Apple Documentation

总结

以上是内存溢出为你收集整理的ios – 使用排除路径计算TextView的单元格高度全部内容,希望文章能够帮你解决ios – 使用排除路径计算TextView的单元格高度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1104786.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存