Ross - That looks like a good solution, basically, I'm highlighting the background versus the actual text. Very clever. This approach has the added advantage of preventing the user from accidentally deleting text because it's not a real highligh. I'll try this out tonight.
Using this code I was able to highlight text in an textView but again, it's highlighting the text, not the background: NSString *s = [textView string]; NSMutableArray *selRangeArray = [[NSMutableArray alloc] init]; if ([s length] > 0) { NSRange r = [s rangeOfString:searchText options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])]; while (r.location != NSNotFound) { [selRangeArray addObject:[NSValue valueWithRange:r]]; NSUInteger newStart = r.location + r.length; r = [s rangeOfString:searchText options:NSCaseInsensitiveSearch range:NSMakeRange(newStart, [s length] - newStart)]; } if ([selRangeArray count] > 0) { [textView setSelectedRanges:selRangeArray]; } } On Sep 6, 2010, at 1:19 PM, Ross Carter wrote: > On Sep 5, 2010, at 7:33 PM, Brad Stone wrote: > >> I want to highlight different substrings contained in controls in a window >> (two different comboBoxes for example) programmatically when the user is >> searching for a subString. I can do this for one comboBox but not both >> simultaneously. Here's one. >> >> NSString *s = [titleComboBox stringValue]; >> NSRange r = [s rangeOfString:searchText options:NSCaseInsensitiveSearch >> range:NSMakeRange(0, [s length])]; >> [titleComboBox selectText:w]; >> if (r.location != NSNotFound) { >> // this is needed to select the substring >> NSText *textEditor = [w fieldEditor:YES forObject:titleComboBox]; >> [textEditor setSelectedRange:r]; >> } >> >> But this only highlights the text in the titleComboBox. If I then repeat >> this to highlight the text in the categoryComboBox by repeating the code, >> titleComboBox gets deselected and categoryComboBox gets selected. I want to >> highlight them both simultaneously. I'd also rather use the big yellow >> highlight like with the find panel (but that's later). >> >> Is this possible? > > Your code will not work because there can be only one active field editor at > a time, and you are controlling the text by manipulating the selectedRange > property of the field editor. > > I would be inclined to try using attributed strings to show the selection. > Maybe something like this: > > if (r.location != NSNotFound) { > NSMutableAttributedString *stringWithSelection = > [[NSMutableAttributedString alloc] initWithString:s]; > [stringWithSelection addAttribute: NSBackgroundColorAttributeName > value:[NSColor yellowColor] range:r]; > // set control value to stringWithSelection, using whatever technique > is appropriate > [stringWithSelection release]; > } > > To deselect, just set the control value to [[control attributedStringValue] > string]. > > There are other ways to do what you need. This might be the most > straightforward. > > -Ross > _______________________________________________ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > Please do not post admin requests or moderator comments to the list. > Contact the moderators at cocoa-dev-admins(at)lists.apple.com > > Help/Unsubscribe/Update your Subscription: > http://lists.apple.com/mailman/options/cocoa-dev/cocoa-dev%40softraph.com > > This email sent to cocoa-...@softraph.com _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com