9

OK, so this is what I want :

  • We have an NSTextView
  • Get "current" word (as an NSRange?), at cursor position (how could this be determined?)
  • Highlight it (change its attributes)

I'm not sure how to go about this : I mean my main concern is getting current position within NSTextView and getting the word at point (I know some Text plugins support that, but I'm not sure as of the original NSTextView implementation...)

Is there any built-in function for that? Or, if not, any ideas?


UPDATE : Cursor position (SOLVED)

NSInteger insertionPoint = [[[myTextView selectedRanges] objectAtIndex:0] rangeValue].location;

Now, still trying to find a workaround for a specifying the underlying word...

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • 1
    I'm really curious why this question got a '-1'. Not being able to understand/answer one's question is not enough of a reason for downvoting it. Or at least a comment would be more constructive... huh? – Dr.Kameleon Sep 23 '12 at 18:02
  • 1
    I don't know why, either. I'm going to balance it out. – Wevah Sep 23 '12 at 18:09
  • 1
    This is actually a good question. +1. –  Sep 23 '12 at 18:20

2 Answers2

8

Here's one way:

NSUInteger insertionPoint = [myTextView selectedRange].location;
NSString *string = [myTextView string];

[string enumerateSubstringsInRange:(NSRange){ 0, [string length] } options:NSStringEnumerationByWords usingBlock:^(NSString *word, NSRange wordRange, NSRange enclosingRange, BOOL *stop) {
if (NSLocationInRange(insertionPoint, wordRange)) {
    NSTextStorage *textStorage = [myTextView textStorage];
    NSDictionary *attributes = @{ NSForegroundColorAttributeName: [NSColor redColor] }; // e.g.
    [textStorage addAttributes:attributes range:wordRange];
    *stop = YES;
}}];
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
Wevah
  • 28,182
  • 7
  • 83
  • 72
  • 1
    Oh, I didn't do the highlighting. Appending… – Wevah Sep 23 '12 at 19:05
  • Thanks a lot. **Great answer**. (I took the liberty to correct one-or-two little bits that were not working). Now one more for you : any idea how I could hook up a selector to the `NSTextView` triggered when cursor position changes? (either with the keyboard or mouse). Should I use some sort of observer? – Dr.Kameleon Sep 23 '12 at 19:11
  • I was going to post that I didn't test it, but then I got distracted. – Wevah Sep 23 '12 at 19:18
  • Oh, we all know how easy it is to overlook a bracket or sth-like-that... No worries! ;-) – Dr.Kameleon Sep 23 '12 at 19:19
  • 2
    For checking for cursor-position changes, you should observe the `NSTextViewDidChangeSelectionNotification` notification, and check to make sure the length of the new selected range is 0. – Wevah Sep 23 '12 at 19:21
  • Well, I'll try that right now. Thanks a lot, mate! I really appreciate it. :-) – Dr.Kameleon Sep 23 '12 at 19:23
1

Simple algorithm to find the word's boundaries (assuming that words are space-spearated):

NSInteger prev = insertionPoint;
NSInteger next = insertionPoint;

while([[[myTextView textStorage] string] characterAtIndex:prev] != ' ')
    prev--;

prev++;

while([[[myTextView textStorage] string] characterAtIndex:next] != ' ')
    next++;

next--;

NSRange currentWordRange = NSMakeRange(prev, next - prev + 1);
NSString *currentWord = [[[myTextView textStorage] string] substringWithRange:currentWordRange];