-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJLCellEditor.m
More file actions
114 lines (87 loc) · 3.48 KB
/
JLCellEditor.m
File metadata and controls
114 lines (87 loc) · 3.48 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
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
//
// JLCellEditor.m
// LanguageBuddy
//
// Created by jlopez on 7/11/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "JLCellEditor.h"
@interface JLCellEditor () <UITextViewDelegate>
@end
@implementation JLCellEditor
@synthesize delegate;
@synthesize tableView;
- (void)dealloc {
[textView release];
[super dealloc];
}
- (void)editCellAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *label = cell.detailTextLabel;
// If already editing this cell, do nothing
if ([textView superview] == [label superview])
return;
[self endCellEditing];
if (textView == nil) {
textView = [[UITextView alloc] initWithFrame:CGRectZero];
textView.delegate = self;
}
CGRect leftLabelFrame = cell.textLabel.frame;
CGRect frame = label.frame;
CGFloat x = leftLabelFrame.origin.x + leftLabelFrame.size.width;
CGFloat scale = [[UIScreen mainScreen] scale];
CGFloat dy = floorf(8.5 * scale) / scale;
textView.frame = CGRectMake(x, frame.origin.y - dy,
frame.origin.x - 8 + frame.size.width + 16 - x,
frame.size.height + 16);
textView.text = label.text;
textView.textColor = label.textColor;
textView.font = label.font;
textView.contentInset = UIEdgeInsetsZero;
textView.textAlignment = UITextAlignmentRight;
textView.autocorrectionType = UITextAutocorrectionTypeNo;
int ix = indexPath.section * 10 + indexPath.row;
textView.keyboardType = ix == 1 ? UIKeyboardTypeNumbersAndPunctuation : UIKeyboardTypeURL;
[label.superview insertSubview:textView aboveSubview:label];
label.hidden = YES;
[textView becomeFirstResponder];
}
- (void)endCellEditing {
// If not editing do nothing
if (![textView isFirstResponder])
return;
[textView resignFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView_ {
// Update settings
NSLog(@"[%@ %@]", [self class], NSStringFromSelector(_cmd));
CGPoint pt = [tableView convertPoint:CGPointZero fromView:textView_];
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:pt];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.detailTextLabel.text = textView_.text;
cell.detailTextLabel.hidden = NO;
[textView_ removeFromSuperview];
if ([delegate respondsToSelector:@selector(tableViewCellDidEndEditingWithText:indexPath:)])
[delegate tableViewCellDidEndEditingWithText:textView_.text indexPath:indexPath];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
- (BOOL)textView:(UITextView *)textView_ shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
// If <enter> was pressed, leave editing mode
if ([text isEqualToString:@"\n"]) {
[self endCellEditing];
return NO;
}
// Disallow insertion of control characters (via paste, usually)
NSRange rng = [text rangeOfCharacterFromSet:[NSCharacterSet controlCharacterSet]];
if (rng.location != NSNotFound)
return NO;
// Prevent text from groing past available textView width
NSString *modifiedString = [textView_.text stringByReplacingCharactersInRange:range withString:text];
CGSize sz = [modifiedString sizeWithFont:textView_.font];
if (sz.width >= textView_.frame.size.width - 16)
return NO;
// All other edits are valid
NSLog(@"[%@ %@] %@ %@", [self class], NSStringFromSelector(_cmd), NSStringFromRange(range), text);
return YES;
}
@end