Reading from RTF file

Discussion in 'iOS Programming' started by electronica, Dec 28, 2011.

  1. electronica, Dec 28, 2011
    Last edited by a moderator: Dec 28, 2011

    electronica macrumors newbie

    Joined:
    Sep 24, 2011
    #1
    hi, i really can't find a solutions for this :(

    what I'm want to do is, read a text from a rich text file and show it in a TextView.
    When reading a txt file I'm using this code and its ok:
    Code:
    NSString *hayat = [[NSBundle mainBundle] pathForResource:@"textbaslik" ofType:@"txt"];
        textview2.text = [NSString stringWithContentsOfFile:hayat encoding:NSUTF8StringEncoding error:nil];
    for reading rtf, i read references and i try to use this code but it won't work:
    Code:
    NSString *hayat = [[NSBundle mainBundle] pathForResource:@"textbaslik" ofType:@"rtf"];
    
    NSMutableAttributedString *mstring  = [[NSMutableAttributedString alloc] initWithPath: path documentAttributes: NULL];
    it seems i can't use the initWithPath method.

    is there any solution to read rtf basically?
    please help..
     
  2. dejo, Dec 28, 2011
    Last edited: Dec 28, 2011

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    It seems you have not shown us how and when path is set.

    EDIT:

    Plus, I don't believe the Foundation Framework can handle RTF. From the NSAttributedString Class Reference:

     
  3. electronica, Dec 28, 2011
    Last edited by a moderator: Dec 28, 2011

    electronica thread starter macrumors newbie

    Joined:
    Sep 24, 2011
    #3
    sorry i did post the altered code. here is the correct one. so "path" is "hayat".

    Code:
    NSString *hayat = [[NSBundle mainBundle] pathForResource:@"textbaslik" ofType:@"rtf"];
    NSMutableAttributedString *mstring  = [[NSMutableAttributedString alloc] initWithPath: hayat documentAttributes: NULL];

    Well. than how can i solve this problem, i didn't get the point.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    AFAIK, the only built-in support for rtf files on iOS is in UIWebView, which can display rtf files.
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #7
    By "dialog" it is meant the message text field that you type into. I edited that post to be slightly more clear.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Just to be clear, there is no need to select any text before using any of these icons. It is no problem to simply click the icon; the desired tags will be inserted, and your cursor will appear in between them, ready for you to type / paste / whatever away.
     
  8. electronica thread starter macrumors newbie

    Joined:
    Sep 24, 2011
    #9
    ok, sorry for this problem.

    now I'm calling my rtf file in a WebView. its ok but not working as textview.
    i can't change my web view's height to length of my text. as i said i have many files and their length is different from each other.
    i made some search and try to solve this with like this

    Code:
    -(void)webViewDidFinishLoad:(UIWebView *)webView{
        [self performSelector:@selector(calculateWebViewSize) withObject:nil afterDelay:0.1];
    }
    - (void) calculateWebViewSize {
         [webview sizeToFit];
        scrollview.contentSize = CGSizeMake(320, webview.frame.size.height);
    }
    
    - (void)viewDidLoad
    {
        NSString *hayat2 = [[NSBundle mainBundle] pathForResource:@"aaa" ofType:@"rtf"];
        NSURL *url = [NSURL fileURLWithPath:hayat2];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [web loadRequest:request];
    
        scrollview.delegate = self;
       // scrollview.contentSize = CGSizeMake(320, webview.frame.size.height);
    }
    well, I'm not get the point where should i set the scrollview's content size.

    I'm try to make scrollview height to webview's contentsize height. Because I'm also disable user interactions of webview so users can only scroll down with scrollview.
    sorry for bad english, i hope you could understand me :(
    please show me the way.
     
  9. electronica thread starter macrumors newbie

    Joined:
    Sep 24, 2011
    #10
    after a lot of search and try, i solved some part of the issue. But not all.

    Firstly, i write my code again and correct webviews delegate and thats worked nice for the setting the webviews height to my text's height. (text is in the rtf file.)

    here is the code:
    Code:
    -(void)webViewDidFinishLoad:(UIWebView *)webView {
        [self performSelector:@selector(calculateWebViewSize)];
    }
    
    - (void) calculateWebViewSize {
        [web sizeToFit];
        scroll.contentSize = CGSizeMake(320, web.frame.size.height + 250);
    }
    
    - (void)viewDidLoad {
        punto = 160;
        arkaplan = 1;
        scroll.delegate = self;
        web.delegate = self;
        
        NSString *hayat2 = [[NSBundle mainBundle] pathForResource:@"aaa" ofType:@"rtf"];
        NSURL *url = [NSURL fileURLWithPath:hayat2];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [web loadRequest:request];
    
        [super viewDidLoad];
    }
    Then i add 2 buttons (A- , A+) for increasing and decreasing the font size and their methods. Also i use "sizetofit" in these methods again because when i change the font size, text length is also change.
    Code:
    -(void)textbuyut:(id)sender{
        if (punto <240) {
            punto = punto + 20;
            NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", punto];
            [web stringByEvaluatingJavaScriptFromString:jsString];
            //NSString *output = [web stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];
            //NSLog(@"height: %d", [output intValue]);
            [web sizeToFit];
            scroll.contentSize = CGSizeMake(320, web.frame.size.height + 250);
        }
    }
    
    -(void)textkucult:(id)sender{
        if (punto > 120) {
            punto = punto - 20;
            NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", punto];
            [web stringByEvaluatingJavaScriptFromString:jsString];
            [web sizeToFit];
            scroll.contentSize = CGSizeMake(320, web.frame.size.height + 250);
        }
    }
    so there isn't any problems at increasing and decreasing the font size, its just working fine. But i have 2 really weird problem here.

    1- in the "textbuyut" method there are 2 lines of code which disabled (Normally they are enabled). So if i delete these 2 lines because they are totally unnecessary, my webview stops to adjust its size(lenght) to my text size. i really don't get whats happening here.

    2- when i click my A- button (calling textkucult), there is no problem at decreasing the font size, but when i click second time, magically my webviews width is increasing, which i never write a single code about this.

    any ideas, solutions? i really appreciate any help.
     

Share This Page