Mac osx开发 NSButton 修改背景色,字体颜色,设置圆角

1.设置NSButton的背景色

_btn.wantsLayer = YES;

 _btn.layer.backgroundColor = [NSColor blueColor].CGColor;

2.设置NSButton文字和字体颜色(应该还有其他方法修改按钮的字体颜色,这里只是参考)

[self setBtnTitleColorwithColor:[NSColor whiteColor] andStr:@"Login" andBtn:_btn];

-(void)setBtnTitleColorwithColor:(NSColor*)color andStr:(NSString*)str andBtn:(NSButton*)btn{

    NSMutableParagraphStyle *pghStyle = [[NSMutableParagraphStyle alloc] init];

    pghStyle.alignment = NSTextAlignmentCenter;

    // 创建Attributes,设置颜色和段落样式

    NSDictionary *dicAtt = @{NSForegroundColorAttributeName:color, NSParagraphStyleAttributeName: pghStyle};


    btn.title=@" ";

    NSMutableAttributedString *attTitle = [[NSMutableAttributedString alloc] initWithAttributedString:btn.attributedTitle];

    // 替换文字

    [attTitlereplaceCharactersInRange:NSMakeRange(0, 1) withString:str];

    // 添加属性

    [attTitleaddAttributes:dicAtt range:NSMakeRange(0, str.length)];

    btn.attributedTitle= attTitle;

}


3.设置NSButton圆角

    [_btn.layer setCornerRadius:16];

    [_btn.layer setMasksToBounds:YES];

你可能感兴趣的:(Mac osx开发 NSButton 修改背景色,字体颜色,设置圆角)