Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

electronica

macrumors newbie
Original poster
Sep 24, 2011
17
0
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..
 
Last edited by a moderator:
it seems i can't use the initWithPath method.

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:

The Foundation framework defines only the basic functionality for attributed strings; in Mac OS X, additional methods supporting RTF, graphics attributes, and drawing attributed strings are described in NSAttributedString Additions, found in the Application Kit.
 
Last edited:
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.
 
Last edited by a moderator:
The contents of that post is inaccurate. It says you can click the Image button and paste into a dialog. No such thing happens.

The way it really works is the same as the Bold, Italic, or Underline buttons. Namely, you select the range of text you want to be enclosed in code tags, then click the button.

I mention this here because I've occasionally seen mismatched code tags, written in lower-case, and wondered how that happened (the icon uses upper-case). The apparent answer is they were manually written out.

Please edit the referenced post, or link to another one, so the description of how it works is accurate.

By "dialog" it is meant the message text field that you type into. I edited that post to be slightly more clear.
 
The way it really works is the same as the Bold, Italic, or Underline buttons. Namely, you select the range of text you want to be enclosed in code tags, then click the button.

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.
 
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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.