9

我正在为应用程序开发文本编辑器。我正在使用 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;

文本大小约为 400k 字。

我面临以下问题。

  • 当我向下滚动时,文本滚动变得太慢,有时应用程序由于内存问题而崩溃。我认为iOS在向下滚动textview时将渲染的文本图像保存在其内存中,但是当我向上滚动到顶部时它会释放内存。

  • 如果我点击“全选”,则选择文本需要花费太多时间,并且在文本选择后滚动变得很差,并且有时应用程序由于内存问题而崩溃,因为它的内存增加了。我认为 iOS 在其内存中生成完整文本的图像(好像它对用户可见),然后选择完整的文本并保留其图像,直到选择完成。选择完成后,应用程序会保留内存。

显示大文本的另一种方法是使用多个文本视图并将文本分配给仅像 UITableView 这样的可见文本视图,但这会增加复杂性,因为我必须重新计算 UItextView 的 layoutManager 的每个 textChanged 委托调用所需的文本视图数量.

任何人都知道如何在 UITextView 中以更好的性能显示大型属性文本。

猜猜 iPages 应用程序是如何工作的,因为它会在区域处于可见范围内时显示文本。

4

1 回答 1

-2

您不应该直接加载整个文本。您应该加载两倍于 textview 容量的文本。

现在跟踪滚动事件并动态修改字符串。

于 2014-02-18T11:37:24.337 回答