 |
 |
Simple text search...
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
As you probably know, im a newb. Could you elaborate?
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
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'."
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |