如何从CGSIZE获取NSAttributedString用于UITextView?

Man*_*ran 16 objective-c uitextview nsattributedstring ios

我正在使用UITextView和创建一个阅读器应用程序NSAttributedString,我需要将整个attributedString分成少量的属性字符串以启用Pages Concept.

我有计算给定属性字符串的帧大小的方法.

        CGRect rect = [attrString boundingRectWithSize:CGSizeMake(768, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
Run Code Online (Sandbox Code Playgroud)

但我需要获取(768,1024)ContentSize的attributeString.

我已经使用了这个,但是attributesStrings没有正确划分,因为它包含NSTextAttachment和HTML蜇.

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)delegate.attributedString);
Run Code Online (Sandbox Code Playgroud)

我该如何计算呢?它需要很快,需要占用少量内存.

And*_*rea 7

如果你想知道a的大小,UITextView你不应该问文本的边界矩形,因为它不是视图的大小.UITextView包含填充,你需要考虑到这一点.
最简单的方法是sizeToFit在添加文本后立即调用文本视图.后来只是要求UItextView框架.