I am customizing the user interface for one of my applications, and the idea is that the text area initially borders on gray when it is out of focus and when it focuses, the border becomes bright white. My app uses a dark theme, and for a single-line NSTextField this works fine.
I am having problems with a subclass of NSTextView . To change the border correctly, I had to actually subclass the parent NSScrollView , but I still see strange behavior. (See Screenshot below.) I want a red border to fill the entire scroll view, as this will allow me to smooth (rather than fill that just for testing) the path, creating a nice border, Instead, the red box seems to only fill internal children's performance.
The following code snippet, which is a subclass of NSScrollView :
- (void)drawRect:(NSRect)dirtyRect { [super drawRect:dirtyRect]; NSRect borderRect = self.bounds; borderRect.origin.y += 1; borderRect.size.width -= 1; borderRect.size.height -= 4; BOOL inFocus = ([[self window] firstResponder] == self); if (!inFocus) { inFocus = [self anySubviewHasFocus:self]; } if (inFocus) { [[NSColor colorWithDeviceRed:.8 green:.2 blue:0 alpha:1] set]; } else { [[NSColor colorWithDeviceRed:.1 green:.8 blue:0 alpha:1] set]; } [NSGraphicsContext saveGraphicsState]; [[NSGraphicsContext currentContext] setShouldAntialias:NO]; [NSBezierPath fillRect:borderRect]; [NSGraphicsContext restoreGraphicsState]; NSLog(@"My bounds: %@", NSStringFromRect(borderRect)); NSLog(@"Super (%@) bounds: %@", [self superview], NSStringFromRect(borderRect)); }
Creates a screenshot as shown below. Also see Log Output, which assumes that the entire view needs to be populated. This is the only result that is ever displayed, regardless of the size of the text inside. Entering carriage returns increases the height of the red , but does not create another output. (And I would like the red box to fill all the borders.)
2011-04-08 21:30:29.789 MyApp[6515:903] My bounds: {{0, 1}, {196, 87}} 2011-04-08 21:30:29.789 MyApp[6515:903] Super (<EditTaskView: 0x3a0b150>) bounds: {{0, 1}, {196, 87}}

Edit: Thanks to Josh Caswell for his answer. See the correct behavior below when it is not focused and when focusing.

