Skip to content

Instantly share code, notes, and snippets.

@Rm1210
Created September 27, 2013 02:04
Show Gist options
  • Save Rm1210/6723257 to your computer and use it in GitHub Desktop.
Save Rm1210/6723257 to your computer and use it in GitHub Desktop.
Apple's set NSButton TextColor Extended
#import <Foundation/Foundation.h>
@interface NSButton (NSButton_Extended)
- (NSColor *)textColor;
- (void)setTextColor:(NSColor *)textColor;
@end
#import "NSButton+Extended.h"
@implementation NSButton (NSButton_Extended)
- (NSColor *)textColor
{
NSAttributedString *attrTitle = [self attributedTitle];
int len = [attrTitle length];
NSRange range = NSMakeRange(0, MIN(len, 1)); // get the font attributes from the first character
NSDictionary *attrs = [attrTitle fontAttributesInRange:range];
NSColor *textColor = [NSColor controlTextColor];
if (attrs)
{
textColor = [attrs objectForKey:NSForegroundColorAttributeName];
}
return textColor;
}
- (void)setTextColor:(NSColor *)textColor
{
NSMutableAttributedString *attrTitle =
[[NSMutableAttributedString alloc] initWithAttributedString:[self attributedTitle]];
int len = [attrTitle length];
NSRange range = NSMakeRange(0, len);
[attrTitle addAttribute:NSForegroundColorAttributeName value:textColor range:range];
[attrTitle fixAttributesInRange:range];
[self setAttributedTitle:attrTitle];
[attrTitle release];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment