初始化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是空字符串的話,也會內(nèi)存泄漏
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鼠標方法的詳解
關(guān)于Mac開發(fā)的隨筆記(以下皆是作者隨筆)
此文已由作者授權(quán),轉(zhuǎn)載請注明文章出處