Re: Syntax coloring a NSTextView
Re: Syntax coloring a NSTextView
- Subject: Re: Syntax coloring a NSTextView
- From: Douglas Davidson <email@hidden>
- Date: Mon, 22 Oct 2001 10:22:44 -0700
NSTextView maintains the notion of the "typingAttributes", which are the
attributes to be applied to the next character typed in. This is what
makes sure, for example, that when you are at the start of an empty
document, choose "Bold", and start typing, the characters you type will
be bold.
If the range that you are replacing agrees exactly with the selected
range, then -[NSTextView replaceCharactersInRange:withString:] will
apply the typing attributes to it. That is probably what you want, even
in this case, for attributes other than the color.
The obvious minimal suggestion in this case is to call
replaceCharactersInRange:withString: first, and then call
setTextColor:range: on a range including both dashes.
Douglas Davidson
On Saturday, October 20, 2001, at 08:51 PM, Johann Hibschman wrote:
Hi folks,
I'm trying to include a basic editor in one of my apps, and I'd like to
have active syntax coloring, but I can't seem to get it to work. I
want "--" to set off a colored comment.
In pseudocode, I'm doing something like
- (BOOL)textView:(NSTextView *)aTextView
shouldChangeTextInRange:(NSRange)aRange
replacementString:(NSString *)str
{
loc = aRange.location; len = aRange.length;
if (<we typed "-", and the previous character is "-">) {
// Color the first "-" red.
[aTextView setTextColor: [NSColor redColor] range:
NSMakeRange(loc-1, 1)];
// Insert the next one. This should also be red, but isn't.
[aTextView replaceCharactersInRange:NSMakeRange(loc, len)
withString:str];
return NO; // Already inserted the text.
}
return YES;
}
(And yes, I know this isn't general, but I want to understand this very
simplistic version before I try to be exactly correct.)
Now, what happens is that the first "-" is colored red, but the
second "-" remains black. This goes against the docs for
replaceCharactersInRange:withString:, which claim that the characters
inserted, if the length of the range is 0, should take the
characteristics of the first character in the range. For some reason,
that doesn't happen here.
(By the way, I have tried not inserting the second "-" and returning
YES. It doesn't change anything.)
Is there some magic going on that I'm unaware of? Does anyone have a
better suggestion for how to do this? I'd subclass NSTextView and
override the replace function, but the docs heavily suggest _not_
subclassing NSTextView.
Thanks,
--Johann
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev