forked from soffes/cheddar-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDITableViewCell.m
156 lines (115 loc) · 4.54 KB
/
CDITableViewCell.m
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// CDITableViewCell.m
// Cheddar for iOS
//
// Created by Sam Soffes on 3/31/12.
// Copyright (c) 2012 Nothing Magical. All rights reserved.
//
#import "CDITableViewCell.h"
#import "CDISettingsViewController.h"
#import "UIColor+CheddariOSAdditions.h"
#import "UIFont+CheddariOSAdditions.h"
@interface CDITableViewCell () <UIGestureRecognizerDelegate>
@property (nonatomic, strong, readonly) UITapGestureRecognizer *editingTapGestureRecognizer;
@property (nonatomic, strong, readonly) UILongPressGestureRecognizer *editingLongPressGestureRecognizer;
@end
@implementation CDITableViewCell
@synthesize editingText = _editingText;
@synthesize textField = _textField;
@synthesize editingTapGestureRecognizer = _editingTapGestureRecognizer;
@synthesize editingLongPressGestureRecognizer = _editingLongPressGestureRecognizer;
- (UITextField *)textField {
if (!_textField) {
_textField = [[SSTextField alloc] initWithFrame:CGRectZero];
_textField.textColor = self.textLabel.textColor;
_textField.placeholderTextColor = [UIColor cheddarLightTextColor];
_textField.backgroundColor = [UIColor whiteColor];
_textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_textField.returnKeyType = UIReturnKeyDone;
_textField.alpha = 0.0f;
[self updateFonts];
[self.contentView addSubview:_textField];
}
return _textField;
}
- (void)setEditingText:(BOOL)editingText {
_editingText = editingText;
if (_editingText) {
[self.contentView addSubview:self.textField];
[self setNeedsLayout];
[_textField becomeFirstResponder];
[UIView animateWithDuration:0.2 delay:0.0f options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{
_textField.alpha = 1.0f;
} completion:nil];
} else {
[_textField resignFirstResponder];
[UIView animateWithDuration:0.2 delay:0.0f options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{
_textField.alpha = 0.0f;
} completion:^(BOOL finished) {
[_textField removeFromSuperview];
_textField = nil;
}];
}
}
#pragma mark - Class Methods
+ (CGFloat)cellHeight {
return 51.0f;
}
#pragma mark - UIView
- (void)layoutSubviews {
[super layoutSubviews];
CGSize size = self.contentView.bounds.size;
if (self.editing) {
_textField.frame = CGRectMake(10.0f, 1.0f, size.width - 46.0f, size.height - 2.0f);
}
}
#pragma mark - UITableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier])) {
self.textLabel.textColor = [UIColor cheddarTextColor];
[self updateFonts];
SSBorderedView *background = [[SSBorderedView alloc] initWithFrame:CGRectZero];
background.backgroundColor = [UIColor whiteColor];
background.bottomBorderColor = [UIColor colorWithWhite:0.92f alpha:1.0f];
background.contentMode = UIViewContentModeRedraw;
self.backgroundView = background;
self.contentView.clipsToBounds = YES;
_editingTapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
_editingTapGestureRecognizer.delegate = self;
[self addGestureRecognizer:_editingTapGestureRecognizer];
_editingLongPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
_editingLongPressGestureRecognizer.delegate = self;
[self addGestureRecognizer:_editingLongPressGestureRecognizer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateFonts) name:kCDIFontDidChangeNotificationName object:nil];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (!selected) {
self.textLabel.backgroundColor = [UIColor whiteColor];
}
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
_editingTapGestureRecognizer.enabled = editing;
}
- (void)prepareForReuse {
[super prepareForReuse];
[self setEditingText:NO];
}
#pragma mark - Font Handling
- (void)updateFonts {
_textField.font = self.textLabel.font;
self.textLabel.font = [UIFont cheddarFontOfSize:18.0f];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return [touch.view isKindOfClass:[UIControl class]] == NO;
}
#pragma mark - Gesture Actions
- (void)setEditingAction:(SEL)editAction forTarget:(id)target {
[_editingTapGestureRecognizer addTarget:target action:editAction];
[_editingLongPressGestureRecognizer addTarget:target action:editAction];
}
@end