Since in practice the NSText* returned by -currentEditor for an NSTextField is always an NSTextView*, I added the following code to my custom NSTextField subclass:
-(BOOL) becomeFirstResponder
{
BOOL success = [super becomeFirstResponder];
if( success )
{
// Strictly spoken, NSText (which currentEditor returns) doesn't
// implement setInsertionPointColor:, but it's an NSTextView in practice.
// But let's be paranoid, better show an invisible black-on-black cursor
// than crash.
NSTextView* textField = (NSTextView*) [self currentEditor];
if( [textField respondsToSelector: @selector(setInsertionPointColor:)] )
[textField setInsertionPointColor: [NSColor whiteColor]];
}
return success;
}
So if you're already replacing this class because you're doing custom background drawing, this might be a more encapsulated solution. Maybe there's even a way to move this up into NSCell, which would be cleaner since NSCell is the one doing the drawing and knowing the colors anyway.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…