NSButton 은 글자 색을 바꿀 수 없게 되어있다. 왜 이렇게 해놨는지 이해가 안된다.
그래서 약간 돌아가야한다.
먼저 NSButton을 상속받은 클래스를 만든다.
#import <Cocoa/Cocoa.h> IB_DESIGNABLE @interface ColoredButton : NSButton @property IBInspectable NSColor *titleColor; @end
#import "ColoredButton.h" @implementation ColoredButton -(void)awakeFromNib { NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[self attributedTitle]]; NSRange titleRange = NSMakeRange(0, [colorTitle length]); [colorTitle addAttribute:NSForegroundColorAttributeName value:self.titleColor range:titleRange]; [self setAttributedTitle:colorTitle]; } - (void)drawRect:(NSRect)dirtyRect { [super drawRect:dirtyRect]; // Drawing code here. } @end
그 다음 버튼의 클래스를 ColoredButton으로 바꾼다.
그럼 이렇게 색을 바꿀 수 있는 걸 사용할 수 있다.
실행시키면 바꾼 색으로 바뀐다.