Re: Fuzzy Focus Ring
Re: Fuzzy Focus Ring
- Subject: Re: Fuzzy Focus Ring
- From: Jerry Krinock <email@hidden>
- Date: Thu, 28 Jan 2010 12:25:46 -0800
On 2010 Jan 28, at 10:51, Charles Jenkins wrote:
> I'd like the active knob to have a nice, fuzzy focus ring, just like a normal Aqua control. And it should appear blue or graphite color according to the user's preference setting.
So, just to be clear, you don't want a Fuzzy Focus Ring. You want a Normal Focus Ring. Normal Focus Rings are fuzzy.
> Considering that the knob will probably be oddly shaped, how do I get the system to draw the focus ring? Is the system focus ring something you can just add to any graphic?
Not that I know of. The only way I've ever seen it done is to trace, or as they say "stroke" the path outlining the control. To do that, you'll need to describe the outline of your control as a bezier path.
Here's a subclass of NSView which I use to draw focus rings. Drawing in Cocoa is kind of weird; be prepared to suspend disbelief. The drawing is done by the NSFrameRect() function, which won't work for you because it only draws rectangles. Instead, you'll need to define a bezier path and then "stroke" it.
Oh, it looks like you can eliminated most of the code below if you're targetting Mac OS 10.5+.
#import <Cocoa/Cocoa.h>
@interface NSView (FocusRing)
- (void)patchPreLeopardFocusRingDrawingForScrolling ;
- (void)drawFocusRing ;
// Although the above method invokes -lockFocus, and thus will work if
// invoked while not within -drawRect, it is recommended to invoke this
// method from within -drawRect, to avoid the possibility of a
// later invocation of -drawRect by Cocoa for some other purpose, which it
// does frequently, will wipe out the focus ring that has just been drawn.
// This can happen even before the focus ring has a chance to show!
@end
@implementation NSView (FocusRing)
// Invoke the following metod during -awakeFromNib
- (void)patchPreLeopardFocusRingDrawingForScrolling {
if (NSAppKitVersionNumber < 900) {
// In Tiger and Panther, the remnants of the focus ring will stay
// on screen as the view is scrolled. The following patch fixes that:
NSView* clipView = self;
while((clipView = [clipView superview]) != nil) {
if([clipView isKindOfClass:[NSClipView class]])
break ;
}
[(NSClipView*)clipView setCopiesOnScroll:NO] ;
}
}
- (void)drawFocusRing {
[self lockFocus] ; // Needed in case we were not invoked from within drawRect:
[[NSColor keyboardFocusIndicatorColor] set];
NSRect rect = [self visibleRect] ;
[NSGraphicsContext saveGraphicsState];
NSSetFocusRingStyle(NSFocusRingOnly);
NSFrameRect(rect);
[NSGraphicsContext restoreGraphicsState];
// The above code is from:
// http://www.cocoabuilder.com/archive/message/cocoa/2003/4/7/88648
// The remainder of that message applies to pre-Leopard only
// and is implemented in this class' -patchPreLeopardFocusRingDrawingForScrolling.
[self unlockFocus] ; // Balance lockFocus
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden