OK, so this is what I want :
NSTextView
NSRange
?), at cursor position (how could this be determined?)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...
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;
}}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With