Changing Syntax Coloring to Objective-C

Update: added changes to UKSyntaxColoredTextViewController implementation.

Changing your editor to syntax color Objective-C is done with some fairly simple changes and additions to your MyDocument class, plus a few to UKSyntaxColoredTextViewController. In MyDocument.m you need to change the syntaxDefinitionFilename method to return @"Objective C", and add two delegate methods that respond to UKSyntaxColoredTextView, the view controller that handles your UKSyntaxColoredTextDocument.

#pragma mark UKSyntaxColoredTextViewDelegate methods

-(NSString *)syntaxDefinitionFilenameForTextViewController: 
               (UKSyntaxColoredTextViewController*)sender {
	return @"Objective C";

-(NSDictionary*) syntaxDefinitionDictionaryForTextViewController: 
    NSBundle* theBundle = [NSBundle mainBundle];
    NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile: 
        [theBundle pathForResource: @"Objective C" ofType:@"plist"]];
    if (!dict) {
        NSLog(@"Failed to find the dictionary");
    return dict;

As we have the app set up now, the file will grab its colors from the default file, not
your edited file. Fixing this requires changing commenting out a few lines in
UKSyntxColoredTextViewController.m. All of the changes are in the recolor

The first line to comment out is line 841 of the file (38 down from the start of recolor):

NSUserDefaults*	vPrefs = [NSUserDefaults standardUserDefaults];

In the while loop just below that, comment out two lines and replace them with the
non-commented line as shown below:

    // Changed vColor assignement to grab color from file, not defaults
//  NSString*   vColorKeyName = [@"SyntaxColoring:Color:" stringByAppendingString: vComponentName];
//  NSColor*	vColor = [[vPrefs arrayForKey: vColorKeyName] colorValue];
    NSColor*	vColor = [[vCurrComponent objectForKey:@"Color"] colorValue];

Compile and run. Open up an Objective-C header or implementation file, and you should see colors for keywords (green) and block comments (red). Now look around your file for strings. You won’t see any, but there are blank spots where the strings ought to be, and if you select those spots, you’ll see that they are in fact there, they were just white characters on a white background. Let’s fix that.

In Xcode, edit the Objective C.plist file. Under Components, select Item 0, and in that, select the Color. Change the three components from (1, 1, 1) to something more suitable; I picked a medium gray, (0.4, 0.4, 0.4). Save, compile and run. This time you should see your strings showing up.

Changing your file to color CSS files is similarly easy: just change the three methods that
select the syntax coloring files to use @"CSS 1" instead of @"Objective C".
It’s worthwhile looking in to the syntax definition plists to see how you can set
them up for any language you want to color. The CSS plist in particular has a long list
of keywords that are the properties you can use in CSS.

So now we have the basics in hand: we can set up documents that can syntax color three different languages, and also see what line and character the cursor is at. Cool!

This entry was posted in Syntax Coloring. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s