huddie96 huddie96 - 1 year ago 94
Objective-C Question

Populating UITextView as it scrolls

Is there anyway to load text from a file into a UITextView in parts yet keep scrolling smooth (Like loading text into a UITableView and splitting it up among multiple cells)?

For Example: I have a RTF (rich-text file) with over 120,000 words in it. Due to attributes and the amount of words, scrolling is choppy.
I want to try a free up memory. Instead of loading the entire text-file at once, I would like to load just enough text to fit on the screen and to make scrolling looks smooth.

I've seen this answer: Reading Specific Part Of A Text File that deals with reading specific parts of a file but my file isn't broken up by special characters in anyway.

Answer Source

Instead of using a UITextView and RTF, you can use a UIWebView and HTML (use a conversion tool to turn HTML into RTF). Here is how to hook it all up and change the text size. You can call the resizeWebViewToSize at any time to resize the text.

- (void)viewDidLoad
    [super viewDidLoad];
    self.webView.delegate = self;

- (void)viewWillAppear:(BOOL)animated
    [super viewWillAppear:animated];

    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"HtmlFile" ofType:@"html"];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    [self.webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]];

- (void)webViewDidFinishLoad:(UIWebView *)webView
    [self resizeWebViewToSize:@"1000"];

- (void)resizeWebViewToSize:(NSString *)size
    NSString *fontSize=size;
    NSString *jsString = [[NSString alloc]      initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",[fontSize intValue]];
    [self.webView stringByEvaluatingJavaScriptFromString:jsString];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download