You may not like the default colors, and different users prefer different colors, so it’s a good idea to let the users select their own colors and have them stored in user preferences. UKSyntaxColoredTextDocument makes that possible with UKSCTDColorWellPrefsController and UKSCTDUserIdentifiersPrefsController. You won’t have to write any new code, although it turns out you do have to undo the changes we made to UKSyntaxColoredTextViewController.
Most of what you need to do is in your MainMenu nib. You’ll be adding an NSPanel with several color wells and a table for user-defined identifiers. I took the easy way out and copied the panel from the MainMenu nib of the sample application that came with UKSyntaxColoredTextDocument, and pasted it into my app’s menu nib.
So start up Interface Builder and open the MainMenu nib of the project that you downloaded, copy the Preferences panel, and close the nib. Now open the MainMenu nib of your project, and paste the panel into nib window.
Now go to the Library and grab two NSObjects. Change the class of one to UKSCTDColorWellPrefsController and the other to UKSCTDUserIdentifiersPrefsController. It’s time to start making connections between these objects and the preferences panel. First, let’s connect the color wells to the color well preference controller. Note that there’s a takeIdentifiersColorFrom and a takeIdentifiers2ColorFrom, along with a takeCommentsColorFrom and a takeComments2ColorFrom.
- Connect the Comments color well to the takeCommentsColorFrom: action.
- Connect the Strings color well to the takeStringsColorFrom: action.
- Connect the Standard Identifiers color well to the takeIdentifiersColorFrom: action.
- Connect the User’s Identifiers color well to the takeIdentifiers2ColorFrom: action.
- Connect the Preprocessor Directives color well to the takePreprocessorColorFrom: action.
- Connect the HTML Tags color well to the takeTagsColorFrom: action.
- Connect the Secondary Comments color well to the takeComments2ColorFrom: action.
- Connect the Reset Defaults button to the resetColors: action.
Next, connect the outlets of the color wells prefs controller by control-dragging from the color wells preference controller to the appropriate color well. Again, note the difference between commentsColor and comments2Color, as well as identifiersColor and identifiersColor.
- Connect the Comments color well to the commentsColor outlet.
- Connect the Strings color well to the stringssColor outlet.
- Connect the Standard Identifiers color well to the identifiersColor outlet.
- Connect the User’s Identifiers color well to the identifiers2Color outlet.
- Connect the Preprocessor Directives color well to the preprocessorColor outlet.
- Connect the HTML Tags color well to the tagsColor outlet.
- Connect the Secondary Comments color well to the comments2Color outlet.
Now it’s time to move on to the user identifiers preference controller, so the users will be able to specify their own identifiers in addition to the ones the default files. Click the table view until you have selected the table view itself (the first click selects the scroll view). Now control-drag over to the user identifiers preference controller to connect it to the dataSource and delegate outlets.
Next up are the three connections for the two buttons, Add and Remove. Control-drag from the Add button to the user identifiers preference controller and select the createNewIdentifier: action, then similarly connect the Remove button to the deleteIdentifier: action. Finally, control-drag from the user identifiers preference controller to the Remove button to connect the removeButton outlet. This allows disabling the Remove button if there are no identifiers to remove.
The last task in IB is to connect the Preferences… menu item to the preferences pane. Control-drag from the menu item to the preferences pane and select the makeKeyAndOrderFront: action.
We have one more task, but it’s in Xcode. Head back to UKSyntaxColoredTextDocument and undo the changes you made (or restore the original if you’re doing version control).
Compile and run your application and load up an Objective-C file (or whichever kind you’ve chosen to color). Now call up the preferences pane, change one of the colors, and head back to your file. You don’t see the changed color, do you? Add or delete a character from your file, which will make the app recolor the file, and you’ll see your change.