Custom NSButtonCell, drawBezelWithFrame not called

断了今生、忘了曾经 提交于 2019-12-23 02:17:29

问题


I'm trying to figure out how to custom draw Buttons in Cocoa/OSX. Since my view is custom drawn I will not use IB and want to do it all in code. I created a subclass of NSButtonCell and a subclass of NSButton. In the Subclass of NSButtonCell I override the Method drawBezelWithFrame:inView: and in the initWithFrame Method of my subclassed NSButton I use setCell to set my CustomCell in the Button. However, drawBezelWithFrame gets not called and I don't understand why. Can someone point out what I've done wrong or what I miss here?

Subclass of NSButtonCell:

#import "TWIButtonCell.h"

@implementation TWIButtonCell

-(void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView
{
    //// General Declarations
[[NSGraphicsContext currentContext] saveGraphicsState];

    //// Color Declarations
    NSColor* fillColor = [NSColor colorWithCalibratedRed: 0 green: 0.59 blue: 0.886 alpha: 1];

    //// Rectangle Drawing
    NSBezierPath* rectanglePath = [NSBezierPath bezierPathWithRect: NSMakeRect(8.5, 7.5, 85, 25)];
    [fillColor setFill];
    [rectanglePath fill];
    [NSGraphicsContext restoreGraphicsState];
}

@end

Subclass of NSButton:

#import "TWIButton.h"
#import "TWIButtonCell.h"

@implementation TWIButton

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        TWIButtonCell *cell = [[TWIButtonCell alloc]init];
        [self setCell:cell];
    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    // Drawing code here.
}

@end

Usage:

- (void)addSendButton:(NSRect)btnSendRectRect 
{
    TWIButton *sendButton = [[TWIButton alloc] initWithFrame:btnSendRectRect];
    [self addSubview:sendButton];
    [sendButton setTitle:@"Send"];
    [sendButton setTarget:self];
    [sendButton setAction:@selector(send:)];
}

回答1:


Following are the things seems to be missing out from your code.

  1. You are not calling the [super drawRect:dirtyRect]
  2. You are not overriding + (Class)cellClass in the Class(TWIButton) which is derived from NSButton.

Below is the code after changes:

@implementation TWIButton

    - (id)initWithFrame:(NSRect)frame
    {
        self = [super initWithFrame:frame];
        if (self)
        {
            TWIButtonCell *cell = [[TWIButtonCell alloc]init];
            [self setCell:cell];
        }

        return self;
    }

    - (void)drawRect:(NSRect)dirtyRect
    {
        // Drawing code here.
       //Changes Added!!!
    [super drawRect:dirtyRect];

    }

    //Changes Added!!!!
    + (Class)cellClass
    {
       return [TWIButtonCell class];
    }

    @end

Now keep the break point at drawBezelWithFrame and check it will get called.




回答2:


One might forgo the NSButton subclass, since it looks like you are ONLY using it to instantiate the Cell type within the initializer. Simply

NSButton *button ...
[button setCell: [[TWIButtonCell alloc] init] autorelease]];

btw. you will probably have a leak in the previous examples since you init and then call setCell that likely has its own retain.



来源:https://stackoverflow.com/questions/16838525/custom-nsbuttoncell-drawbezelwithframe-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!