2

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.

Community
  • 1
  • 1
huddie96
  • 1,240
  • 2
  • 13
  • 26
  • 1
    Have you tried using a webView instead of a textView? – Alex Oct 13 '16 at 14:59
  • @Alex Your a wizard! Side-point: Webview doesn't give the ability for changing text size, text alignment..etc right? - I know you can search for the Tag... but RTF doesn't have a Tag since its not HTML right? – huddie96 Oct 13 '16 at 15:12
  • I wonder if you can convert the RTF to an html doc and then add your formatting? You should be able to add the html elements as text to the rtf text I would think. – Alex Oct 13 '16 at 15:19
  • @Alex http://stackoverflow.com/questions/11049134/convert-rtf-file-into-html-in-objective-c - First answer says its no possible. I guess I could do it outside and just add the HTML File – huddie96 Oct 13 '16 at 15:34
  • Yeah it looks like you'll have to manually edit the RTF, or convert/create it in HTML if you want to use a webView. – Alex Oct 13 '16 at 15:38
  • @Alex Im having a lot of trouble. The choppiness is much better but I can't resize the font or change alignment. So far I have saved the RTF file as a HTML file using textedit on my mac. I then tried different answers on this page: http://stackoverflow.com/questions/589177/how-to-increase-font-size-in-uiwebview but nothing seems to yield a good result. - Do you know anything about HTML/UIWebview that if I opened a new question you could assist? – huddie96 Oct 13 '16 at 15:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125621/discussion-between-alex-and-huddie96). – Alex Oct 13 '16 at 16:10

1 Answers1

1

Instead of using a UITextView and RTF, you can use a UIWebView and HTML (use a conversion tool to turn RTF into HTML). 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];
}
Alex
  • 3,861
  • 5
  • 28
  • 44
  • Resizing as well as initial loading take a considerate amount of time. Anyway to speed it up? – huddie96 Oct 15 '16 at 17:20
  • I asked a new question as this deserves its own: http://stackoverflow.com/questions/40062963/smooth-resizing-of-uiwebview-font – huddie96 Oct 15 '16 at 18:54