Add in the shadowed text field cell.

This commit is contained in:
Danny Greg 2013-02-18 20:12:59 +00:00
Родитель 18c541a794
Коммит 3c763d9a4d
2 изменённых файлов: 93 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,17 @@
//
// RBLShadowedTextFieldCell.h
// Rebel
//
// Created by Danny Greg on 18/02/2013.
// Copyright (c) 2013 GitHub. All rights reserved.
//
#import <Cocoa/Cocoa.h>
extern NSInteger const RBLShadowedTextFieldAllBackgroundStyles;
@interface RBLShadowedTextFieldCell : NSTextFieldCell
- (void)setShadow:(NSShadow *)shadow forBackgroundStyle:(NSBackgroundStyle)backgroundStyle;
@end

Просмотреть файл

@ -0,0 +1,76 @@
//
// RBLShadowedTextFieldCell.m
// Rebel
//
// Created by Danny Greg on 18/02/2013.
// Copyright (c) 2013 GitHub. All rights reserved.
//
#import "RBLShadowedTextFieldCell.h"
#import "NSColor+RBLCGColorAdditions.h"
NSInteger const RBLShadowedTextFieldAllBackgroundStyles = 0xFFFFFFFF;
@interface RBLShadowedTextFieldCell ()
@property (nonatomic, readonly, strong) NSMutableDictionary *backgroundStylesToShadows;
@end
@implementation RBLShadowedTextFieldCell
#pragma mark - Lifecycle
- (void)commonInit {
_backgroundStylesToShadows = [NSMutableDictionary dictionary];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self == nil) return nil;
[self commonInit];
return self;
}
- (instancetype)initTextCell:(NSString *)aString {
self = [super initTextCell:aString];
if (self == nil) return nil;
[self commonInit];
return self;
}
#pragma mark - Drawing
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
[NSGraphicsContext saveGraphicsState];
NSShadow *shadow = self.backgroundStylesToShadows[@(self.backgroundStyle)];
if (shadow == nil) {
shadow = self.backgroundStylesToShadows[@(RBLShadowedTextFieldAllBackgroundStyles)];
}
if (shadow != nil) {
CGContextSetShadowWithColor(NSGraphicsContext.currentContext.graphicsPort, shadow.shadowOffset, shadow.shadowBlurRadius, shadow.shadowColor.rbl_CGColor);
}
[super drawWithFrame:cellFrame inView:controlView];
[NSGraphicsContext restoreGraphicsState];
}
#pragma mark - API
- (void)setShadow:(NSShadow *)shadow forBackgroundStyle:(NSBackgroundStyle)backgroundStyle {
if (shadow == nil) {
[self.backgroundStylesToShadows removeObjectForKey:@(backgroundStyle)];
return;
}
self.backgroundStylesToShadows[@(backgroundStyle)] = shadow;
}
@end