Loading huge Text file in UITextview crashes

耗尽温柔 提交于 2020-01-14 13:59:07

问题


I want to update a huge text file in UITextView. But the device just hangs or crashes some times. The text file size is 4MB.

The UITextView is been added from Interface Builder.

I am loading the file from document directory. Below is the code for loading the text file:-

NSError *err = nil;
txtView.text =[NSString stringWithContentsOfFile:documentPath encoding:NSUTF8StringEncoding error:&err];

As I want to edit the file I cannot load it in WebView which is much faster and it works properly.

Is there any way were I can only partially load the text File(one page at a time) so it doesn't take much time to load or some other way to load the whole file properly ?

Any hint in right direction would be highly appreciated. .


回答1:


Sounds like a nasty problem, because you would have to measure how much text has to fit exactly in one page. You could try and play around with the UIScrollView-properties and delegate methods of the textview (like contentOffset), but I would not recommend it.

Personally, I would try to divide the text content into multiple pages (multiple NSStrings) and then use two buttons "next", "previous" to switch between pages within the text view.




回答2:


Store each line of your data in an Array and use array as data source of UITableView, rest of the things will be handled by UItableview



来源:https://stackoverflow.com/questions/4131412/loading-huge-text-file-in-uitextview-crashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!