Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Displaying a txt file from the internet in a NSTextView?

Displaying a txt file from the internet in a NSTextView?
Thread Tools
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jun 3, 2003, 01:15 PM
 
Is this possible? If so is there any sample code or tutorial?

Im looking to gather the text from a .txt file, and for it to display in a text view. If possible, id rather use a rtf file.
     
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status: Offline
Reply With Quote
Jun 3, 2003, 01:28 PM
 
Originally posted by iOliverC:
Is this possible? If so is there any sample code or tutorial?

Im looking to gather the text from a .txt file, and for it to display in a text view. If possible, id rather use a rtf file.
NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.foo.bar/file.txt"]];
[myTextView setString:string];

Isn't Cocoa great?
Using rtf instead of raw text isn't much harder, check the docs for NSTextView and NSText.
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jun 3, 2003, 03:06 PM
 
I get the code, but im just not sure how to make the text view to display it. Sorry for my newbness. Thanks!
     
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Jun 3, 2003, 04:08 PM
 
//Line 1:


NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.foo.bar/file.txt"]];

//Line 2:

[myTextView setString:string];


Line 1 creates an NSString and initiates it with the indicated URL data. So you end up with an NSString holding your data.

Line 2 says that myTextview is to be sent a message to set its display with the string from Line 1. Substitute myTextView with whatever name you've given your NSTextView widget in Interface Builder and you your are set.

Cocoa is freaking awesome!

     
Banned
Join Date: Apr 2002
Location: -
Status: Offline
Reply With Quote
Jun 3, 2003, 04:11 PM
 
Originally posted by iOliverC:
I get the code, but im just not sure how to make the text view to display it. Sorry for my newbness. Thanks!
you just send the view a - setStringNSString *)string message

e.g.

[myView setString;@"hello"]

The documentation says it all.
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jun 3, 2003, 04:41 PM
 
Hey guys, I got the code and thought back to something similar and put it in awakeFromNib. Now onto the rtf challenge =-)
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:39 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2