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];
网友评论