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 > Simple text search...

Simple text search...
Thread Tools
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jul 4, 2003, 11:27 AM
 
Basically, I want to search a NSTextView from the toolbar, I have a NSTextView set up etc, but I'm having trouble searching a document. I've had a look at Apple's standard search code (TextFinder.m/h) and I can't get it. If theres any chance, can someone post some sample code that searches a NSTextView and highlights the results.

Thanks!
Oliver
     
Senior User
Join Date: Feb 2003
Location: USA
Status: Offline
Reply With Quote
Jul 5, 2003, 08:24 AM
 
I don't have any clue, but the source code for TextEdit is included with the samples and might help you out.

Steve W
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jul 5, 2003, 09:12 AM
 
Ye, thats where I went first. But that search code is more suited to its own seperate nib etc. With replace etc.
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 5, 2003, 04:59 PM
 
Just use [[textView string] rangeOfString: options: range:].

EDIT: Screwed up the method name. D'oh.
(Last edited by Chuckit; Jul 5, 2003 at 05:37 PM. )
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jul 5, 2003, 05:33 PM
 
As you probably know, im a newb. Could you elaborate?
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 5, 2003, 05:58 PM
 
Here's a sample method that will find all instances of a string in a text view. It's not very optimized, but it should give you an idea what you need to do.
Code:
- (NSArray *)findString:(NSString *)searchString { NSTextView *textView; // Assume this exists NSString *textString = [textView string]; NSRange rangeToSearch = { 0, [textString length] }; NSMutableArray *rangeArray = [NSMutableArray arrayWithCapacity:1]; NSRange foundRange = [textString rangeOfString:searchString options:0 range:rangeToSearch]; while ( foundRange.location != NSNotFound) { NSValue *result = [NSValue valueWithRange:foundRange]; [rangeArray addObject:result]; rangeToSearch.location = foundRange.location + foundRange.length; rangeToSearch.length = [textString length] - rangeToSearch.location; foundRange = [textString rangeOfString:searchString options:0 range:rangeToSearch]; } return rangeArray; }
EDIT: Sorry. I really need to pay more attention when I do code samples. That was a bit too quick and dirty....
(Last edited by Chuckit; Jul 5, 2003 at 06:18 PM. )
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
   
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:46 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