macOS 开发 - NSButton


一、创建

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;

在这里插入图片描述


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

在这里插入图片描述


二、修改image/title 的 rect

UIButton 是创建子类继承自 UIButton,然后再子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];
    
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI工程仔

请我喝杯伯爵奶茶~!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值