forked from soffes/cheddar-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDITableViewCell.m
128 lines (98 loc) · 3.57 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
//
// 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 "UIColor+CheddariOSAdditions.h"
#import "UIFont+CheddariOSAdditions.h"
#import <objc/runtime.h>
@interface CDITableViewCell () <UIGestureRecognizerDelegate>
@end
@implementation CDITableViewCell
@synthesize editingText = _editingText;
@synthesize textField = _textField;
@synthesize editingTapGestureRecognizer = _editingTapGestureRecognizer;
- (UITextField *)textField {
if (!_textField) {
_textField = [[SSTextField alloc] initWithFrame:CGRectZero];
_textField.textColor = self.textLabel.textColor;
_textField.placeholderTextColor = [UIColor cheddarLightTextColor];
_textField.font = self.textLabel.font;
_textField.backgroundColor = [UIColor whiteColor];
_textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_textField.returnKeyType = UIReturnKeyDone;
_textField.alpha = 0.0f;
[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.textLabel.font = [UIFont cheddarFontOfSize:20.0f];
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];
}
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 - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return [touch.view isKindOfClass:[UIControl class]] == NO;
}
@end