Selected Menu Item Color not aligned?

I thinks a image is better than a thousand words.

What you see in this picture are NSBox Subclass in a NSViewCollection. I set the fill color to "Selected Menu Item Color" is Interface builder.

Why is the color like that ?

Edit : After reading this SO post, I found out I probably have to set the setPatternPhase. But how/where ?


The "Selected Menu Item Color" pattern is designed to be used on "menu-high" items, and it looks like your NSBox is taller than the pattern is.

That is, you could mess around with the pattern phase origin using code from my answer, but I don't think it will do you any good because the pattern just isn't tall enough for your NSBox.

Inspired by Smilin Brian's solution I came up with this :

-(void) drawRect: (NSRect)dirtyRect
    [super drawRect:dirtyRect];
    if(mouseOver) {
        [NSGraphicsContext saveGraphicsState];

        CGFloat yOffset = NSMaxY([self convertRect:self.bounds toView:nil]);
        CGFloat xOffset = NSMinX([self convertRect:self.bounds toView:nil]);
        [[NSGraphicsContext currentContext] setPatternPhase:NSMakePoint(xOffset, yOffset)];

        [[NSColor selectedMenuItemColor ] setFill];
        [NSGraphicsContext restoreGraphicsState];

For the moment it just handles mouseOver events and not selection but it's pretty much the same.

Just for the who might need that for a NSBox I used :

- (void)awakeFromNib{
    NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds
                                                                options: (NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow )
                                                                  owner:self userInfo:nil];
    [self addTrackingArea:trackingArea];


- (void)mouseEntered:(NSEvent *)theEvent {
    mouseOver = true;
    [self setNeedsDisplayInRect:self.bounds];
    [self needsDisplay];

- (void)mouseExited:(NSEvent *)theEvent {
    mouseOver= false;
    [self setNeedsDisplayInRect:self.bounds];

    [self needsDisplay];

Need Your Help

How to use PHP interact with YouTube API?

php api video upload youtube

I want to let my website's user to upload their video to YouTube, but is through my youtube account.That is, all the video uploaded will store in my YouTube account.

CPU vs. Duration in SQL Server Profiler

sql cpu profiler trace duration


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.