osx - NSTextView: When to automatically insert characters (like auto-matching parenthesis)? -
i've got nstextview , i'm acting nstextstorage delegate, i'm getting callbacks textstoragewillprocessediting: , textstoragedidprocessediting:, , i'm using did callback change attributes on text (coloring words).
what i'd add auto-matching of character pairs. when user types (, want insert ) i'm not sure when or proper time this.
from text storage delegate protocol, says will method lets change text displayed.. i'm not sure means or how can that. text system big , confusing.
how should this?
in open-source project, subclassed nstextview , overrode inserttext: handle insertion of matching characters there. can examine argument inserttext: see if want act on, call super perform normal insertion of text, call inserttext: again appropriate matching character string if needed.
something this:
- (void)inserttext:(id)insertstring { [super inserttext:insertstring]; // if insert string isn't 1 character in length, cannot brace character if ([insertstring length] != 1) return; unichar firstcharacter = [insertstring characteratindex:0]; switch (firstcharacter) { case '(': [super insertstring:@")"]; break; case '[': [super insertstring:@"]"]; break; case '{': [super insertstring:@"}"]; break; default: return; } // adjust selected range since inserted character [self setselectedrange:nsmakerange(self.selectedrange.location - 1, 0)]; }
Comments
Post a Comment