macOS 开发 - NSButton

版权声明:本文为博主原创文章,转载请附上本文链接地址。from : https://blog.csdn.net/lovechris00 https://blog.csdn.net/lovechris00/article/details/83446855


常见创建代码


- (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];
    
}

实用 category


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

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

纯图片按钮

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

设置按钮文字颜色

- (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];
    
}

按钮文字居中

//文字居中
- (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];
    
}

NSButtonConvenience 分类中的类创建方法


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;

在这里插入图片描述




猜你喜欢

转载自blog.csdn.net/lovechris00/article/details/83446855