一、创建

1、常见创建代码

- (void)testBtn{
    
    NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, 100, 100, 45)];
    btn.title = @"标题";                                   // 按钮文字
    btn.image = [NSImage imageNamed:@"icon_norm"];                                   // 按钮图片
    [btn setTarget:self];
    [btn setAction:@selector(btnOnClick)];                              // 按钮触发的方法
    btn.alternateTitle = @"选中";                             // 开启状态文字
    btn.alternateImage = [NSImage imageNamed:@"icon_high"];                           // 开启状态图片
    btn.state = 1;                                     // 按钮的状态
    
    btn.imagePosition = NSImageBelow;                     // 图文位置
    btn.imageScaling = NSImageScaleNone;         // 设置图片缩放
    btn.bordered = NO;                               // 按钮是否有边框
    btn.transparent = NO;                            // 按钮是否透明
    // 以下设置的快捷键为: Shift + Command + I (如果设置的和系统的冲突,则不会触发)
    btn.keyEquivalent = @"I";                             // 快捷键
    [btn setKeyEquivalentModifierMask:NSEventModifierFlagShift]; // 快捷键掩码
    btn.highlighted = YES;                                 // 按钮是否为高亮

    [self.window.contentView addSubview:btn];
    
}

2、NSButtonConvenience 分类中的类创建方法

2.1 buttonWithImage
NSButton *btn = [NSButton buttonWithImage:[NSImage imageNamed:@"pupu"] target:self action:@selector(btnOnClick:)];
btn.frame = CGRectMake(100, 100, 200, 200);
btn.wantsLayer = YES;

blog_nsbutton_001_01

2.2 buttonWithTitle
NSButton *btn = [NSButton buttonWithTitle:@"123" image:[NSImage imageNamed:@"pupu"] target:self action:@selector(btnOnClick:)];
btn.frame = CGRectMake(100, 100, 100, 400);
btn.wantsLayer = YES;

blog_nsbutton_002_01

二、修改image/title 的 rect

NSButton 是创建子类继承自 NSButton,然后再子button 的.m 文件中重写 titleForRect 方法; 在 cocoa 中,很多控件都需要通过重写cell 来修改内部的rect。如下:

1、创建 NaviButtonCell 继承自 NSButtonCell,在 .m 文件中设置 rect:

-(NSRect)imageRectForBounds:(NSRect)rect{
    return NSMakeRect(13, 10, 9, 9);
}

-(NSRect)titleRectForBounds:(NSRect)rect{
    return NSMakeRect(24, 0, rect.size.width - 24, rect.size.height);
}

2、创建 NaviButton 继承自 NSButton,在 .m 中设置 cellClass:

@implementation NaviButton

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

+(Class)cellClass{
    return [NaviButtonCell class];
}


@end

三、实用 category

1、一个简单地没有边框的按钮

- (void)msCommonSetting{
    
    [self msSetLayerColor:[NSColor clearColor]];
    [self setBezelStyle:NSBezelStyleRoundRect];
    self.bordered = NO;
    self.imagePosition = NSImageLeft;
}

2、纯图片按钮

- (void)msPureImageBtn:(NSImage *)image{
 
    [self msCommonSetting];
    
    [self setImage:image];
    self.imageScaling = NSImageScaleNone;
    self.imagePosition =  NSImageOnly;
}

3、设置按钮文字颜色

- (void)msSetButtonTitle:(NSString *)title color:(NSColor*)color{
    
        if(color ==nil) {
            color = kColor_TextBlack;
        }
        NSFont *font = self.font;
        NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:font,
                               NSFontAttributeName,
                               color,
                               NSForegroundColorAttributeName,
                               nil];
        NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:title attributes:attrs];
        [self setAttributedTitle:attributedString];
    
}

4、按钮文字居中

//文字居中
- (void)msSetButtonCenterTitle:(NSString *)title color:(NSColor*)color{
    
    if(color ==nil) {
        color = kColor_TextBlack;
    }
    NSFont *font = self.font;
    NSMutableParagraphStyle *centredStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [centredStyle setAlignment:NSCenterTextAlignment];
  
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,NSParagraphStyleAttributeName,
                           font,NSFontAttributeName,
                           color,NSForegroundColorAttributeName,
                           nil];
    NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:title attributes:attrs];
    [self setAttributedTitle:attributedString];
    
}