初始化NSbutton(这里就不做布局设置了)
NSButton *btn = [[NSButton alloc]init];
设置button风格为Square
[btn setBezelStyle:NSBezelStyleRegularSquare];
设置字体颜色
[self setButtonColor: btn];
- (void)setButtonColor:(NSButton*)btn{
NSMutableParagraphStyle*btnStyle = [[NSMutableParagraphStylealloc]init];
btnStyle.alignment=NSTextAlignmentCenter;
NSDictionary *dicAtt =@{NSForegroundColorAttributeName:[NSColor redColor],NSParagraphStyleAttributeName: btnStyle};
//给NSButton先赋值一个字符串,为的是后面替换,如果NSButton的title是空字符串的话,也会内存泄漏
btn.title=@"清空全部";//这里的字符串长度要和后面的设置的长度一样 这里可以随便填
NSMutableAttributedString*attTitle = [[NSMutableAttributedString alloc]initWithAttributedString:btn.attributedTitle];
//替换文字
[attTitle replaceCharactersInRange:NSMakeRange(0,4)withString:@"清空全部"];
[attTitle addAttributes:dicAttrange:NSMakeRange(0,4)];
btn.attributedTitle= attTitle;
}
如果有兴趣可以看我的Mac鼠标方法的详解
关于Mac开发的随笔记(以下皆是作者随笔)
此文已由作者授权,转载请注明文章出处