Subclassing an NSTextField
- by Hooligancat
Given all the complex things I seem to cover every day, this appears to be a "what the heck am I doing wrong that seems to simple?" scenario!
I would like to subclass an NSTextField to change the background color and text color. For simplicity sake (and to help anyone who hasn't ever subclassed anything before), here is the example of my (simplified) subclass MyNSTextFieldSubclass...
Step 1:
Create the subclass file:
First the header file
@interface MyTextFieldSubclass : NSTextField {
}
@end
And the method file
@implementation MyTextFieldSubclass
-(NSColor *)backgroundColor {
return [NSColor redColor];
}
-(NSColor *)textColor {
return [NSColor yellowColor];
}
@end
Step 2:
Drag an NSTextField to a window in Interface Builder, select the Identity tab in the inspector and select the class MyTextFieldSubclass
Step 3:
Save the IB file, build and run the application
Problem
When I run the build, the text field does not reflect the color subclassing. However, I know the subclass is being called because if I add the following method, it gets called on text changes.
-(void)textDidChange:(NSNotification *)notification {
NSLog(@"My text changed");
}
So why does the color change not occur on the text fields?
I know that I can set the color in IB, but for anyone who has dealt with a lot of UI elements that all need the same styling, subclassing makes life way, way easier.
Ironically, I have never had to subclass an NSTextField before and this one has me stumped.
As usual, any and all help very much appreciated. I'm sure it will turn out to be a "Doh!" moment - just cant see the wood for the trees right now (plus I'm exhausted from watching too much World Cup Football early in the morning which never helps).