简体   繁体   English

如何从CGSIZE获取NSAttributedString用于UITextView?

[英]How to Get NSAttributedString from CGSIZE for UITextView?

I am creating a reader app using UITextView and NSAttributedString , I need to split whole attributedString into smaller amount of attributed string to enable Pages Concept. 我正在使用UITextViewNSAttributedString创建一个阅读器应用程序,我需要将整个attributedString分成少量的属性字符串以启用Pages Concept。

I have method for calculating frame size of given attributed string. 我有计算给定属性字符串的帧大小的方法。

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

But I need to get attributeString for (768, 1024) ContentSize. 但我需要获取(768,1024)ContentSize的attributeString。

I have used this, but the attributedStrings are not correctly divided, because it contains NSTextAttachment and HTML stings. 我已经使用过这个,但是attributesStrings没有正确划分,因为它包含NSTextAttachmentHTML NSTextAttachment

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)delegate.attributedString);

How can I calculated this? 我该如何计算呢? and It need to be fast and need to takes small amount of memory. 它需要很快,需要占用少量内存。

If you want to know the size of a UITextView you should not ask the bounding rect of text, because it is not the size of the view. 如果你想知道UITextView的大小,你不应该问文本的边界矩形,因为它不是视图的大小。 UITextView contains padding and you need to take into account that. UITextView包含填充,您需要考虑到这一点。
The best and easier way, is to call sizeToFit on the text view right after you added the text. 最好也更简单的方法是在添加文本后sizeToFit在文本视图上调用sizeToFit Later just ask for the UItextView frame. 稍后只需要UItextView框架。

你可以使用UITextView函数sizeThatFits:

[textView sizeThatFits:CGSizeMake(768, 1024)]

If you want to split text between « pages », then you need to use the capabilities of the NSLayoutManager and the NSTextContainer classes. 如果要在“pages”之间拆分文本,则需要使用NSLayoutManagerNSTextContainer类的功能。 Basically, you need to create a NSTextContainer instance for every page you wish to display, using the initWithSize: initializer. 基本上,您需要使用initWithSize: initializer为要显示的每个页面创建一个NSTextContainer实例。 Then you register those containers using -[NSLayoutManager addTextContainer:] . 然后使用-[NSLayoutManager addTextContainer:]注册这些容器。

Here is the relevant documentation : https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/CustomTextProcessing/CustomTextProcessing.html#//apple_ref/doc/uid/TP40009542-CH4-SW35 以下是相关文档: https//developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/CustomTextProcessing/CustomTextProcessing.html#//apple_ref/doc/uid/TP40009542-CH4-SW35

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM