This repository was archived by the owner on Dec 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPFLiteColorCell.mm
More file actions
executable file
·78 lines (61 loc) · 2.41 KB
/
PFLiteColorCell.mm
File metadata and controls
executable file
·78 lines (61 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#import "PSTableCell.h"
#import "PSSpecifier.h"
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@interface PFLiteColorCell : PSTableCell
@property (nonatomic, retain) UIView *colorPreview;
- (void)updateCellDisplay;
@end
@interface UIColor ()
+ (NSString *)hexFromColor:(UIColor *)color;
@end
static void PFLiteColorCellNotifCB(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
PFLiteColorCell *cell = (__bridge PFLiteColorCell *)observer;
[UIView animateWithDuration:0.45
animations:^{
[cell updateCellDisplay];
}
completion:nil];
}
@implementation PFLiteColorCell
- (id)initWithStyle:(long long)style reuseIdentifier:(id)identifier specifier:(PSSpecifier *)specifier {
return [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier specifier:specifier];
}
- (PSSpecifier *)specifier {
return [super specifier];
}
- (UIColor *)previewColor {
return [UIColor cyanColor];
}
- (void)updateCellDisplay {
self.colorPreview.backgroundColor = [self previewColor];
self.detailTextLabel.text = [UIColor hexFromColor:[self previewColor]];
self.detailTextLabel.alpha = 0.65;
}
- (void)didMoveToSuperview {
[super didMoveToSuperview];
NSString *notificationId = [[self specifier] propertyForKey:@"NotificationListener"];
if (notificationId) {
CFNotificationCenterRemoveEveryObserver (CFNotificationCenterGetDarwinNotifyCenter(), (void *)self);
CFNotificationCenterAddObserver (CFNotificationCenterGetDarwinNotifyCenter(),
(void *)self,
PFLiteColorCellNotifCB,
(CFStringRef)notificationId,
NULL,
CFNotificationSuspensionBehaviorCoalesce
);
}
self.colorPreview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 29, 29)];
self.colorPreview.tag = 199; // Stop UIColors from overriding the color :P
self.colorPreview.layer.cornerRadius = self.colorPreview.frame.size.width / 2;
self.colorPreview.layer.borderWidth = 2;
self.colorPreview.layer.borderColor = [UIColor lightGrayColor].CGColor;
[self setAccessoryView:self.colorPreview];
[self updateCellDisplay];
}
- (void)dealloc {
[self.colorPreview release];
CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(), (void *)self);
[super dealloc];
}
@end