1. 程式人生 > >macOS 開發 - NSButton

macOS 開發 - NSButton

文章目錄


常見建立程式碼


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

在這裡插入圖片描述