ios - 带有大 NSAttributedString 的 UItextView 的滚动性能问题

标签 ios objective-c uitextview nsattributedstring nslayoutmanager

我正在为一款应用开发文本编辑器。我正在使用 UITextView

查看加载 TextView 的示例代码。

// 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 万字。

我面临以下问题。

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

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

显示大文本的另一种方法是使用多个 TextView 并将文本分配给仅像 UITableView 的可见 TextView ,但这会增加复杂性,因为我必须重新计算每个 textChanged 委托(delegate)调用所需的 TextView 数量UItextView 的布局管理器。

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

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

最佳答案

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

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

关于ios - 带有大 NSAttributedString 的 UItextView 的滚动性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21852314/

相关文章:

ios - 将 iOS 应用程序上传到 AppStore(上传到 App Store 时发生错误。)

ios - XC文本 : query for text in navigation bar

ios - 以编程方式从 iPhone/iPad 连接到采集单元(WIFI 设备)

ios - iOS 中的 EFCircularSlider

iphone - UITextView 带有 "Done"按钮 *和* "Return"键?

objective-c - Xcode:如何访问子类中的方法?

ios - ViewControllers 作为实例变量

iphone - CAAnimation 的问题

ios - UITableView中的UITextView,使用AutoLayout平滑展开

ios - UITextview If条件并添加 `+`符号