【发布时间】:2014-03-18 02:53:29
【问题描述】:
我正在为一个应用开发文本编辑器。我正在使用 UITextView
查看示例代码以加载文本视图。
// Read text from file (around 300k - 400k words)
NSError *error = nil;
NSString *contentOfFile = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"17254" ofType:@"txt"]
encoding:NSUTF8StringEncoding
error:&error];
// Attributes for text
UIFont *font = [UIFont fontWithName:@"Baskerville" size:36.0f];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentJustified;
NSDictionary *attributes = [[NSDictionary alloc] initWithObjectsAndKeys:font, NSFontAttributeName,
[UIColor blackColor], NSForegroundColorAttributeName,
paragraphStyle, NSParagraphStyleAttributeName, nil];
// Create attributed string
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:contentOfFile attributes:attributes];
// Assign to text view
self.textView.attributedText = attributedString;
文本大小约为 40 万字。
我面临以下问题。
当我向下滚动时,文本滚动变得太慢,并且有时应用程序由于内存问题而崩溃。我认为iOS在向下滚动textview时将渲染的文本图像保存在其内存中,但是当我向上滚动到顶部时它会释放内存。
如果我点击“全选”,则选择文本需要花费太多时间,并且在选择文本后滚动变差,有时应用程序由于内存问题而崩溃,因为它的内存增加了。我认为 iOS 在其内存中生成完整文本的图像(好像它对用户可见),然后选择完整的文本并保留其图像,直到选择完成。选择完成后,应用程序会保留内存。
显示大文本的另一种方法是使用多个文本视图并将文本分配给仅像 UITableView 这样的可见文本视图,但这会增加复杂性,因为我必须重新计算每个 textChanged 委托调用所需的文本视图数量UItextView 的 layoutManager。
任何人都知道如何在 UITextView 中以更好的性能显示大型属性文本。
猜猜 iPages 应用程序是如何工作的,因为它会在区域处于可见范围内时显示文本。
【问题讨论】:
-
Pages 使用完全自定义的视图...如果您只是显示文本,您可以使用
UIWebView... 只需将您的文本包装在一些 HTML 标记中进行格式化,然后显示它像这样 -
它是一个编辑器。用户可以编辑字体、颜色和对齐方式。也可以添加分页符和其他一些选项。
标签: ios objective-c uitextview nsattributedstring nslayoutmanager