-
Notifications
You must be signed in to change notification settings - Fork 6
/
STATextView.m
245 lines (187 loc) · 8.27 KB
/
STATextView.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//
// STATextView.m
// STATextField
//
// Created by Aaron Jubbal on 11/21/14.
// Copyright (c) 2014 Aaron Jubbal. All rights reserved.
//
#import "STATextView.h"
#import "STATextViewBase+PrivateHeaders.h"
#import "STACommon.h"
@interface STATextView () {
NSString *_internalPlaceholder;
NSAttributedString *_internalAttributedPlaceholder;
CGFloat _initialYPosition;
NSTimeInterval _keyboardAnimationDuration;
UIViewAnimationCurve _keyboardAnimationCurve;
CGFloat _topOfKeyboardYPosition;
CGFloat _keyboardYPosition;
}
@property (nonatomic, strong) UIBarButtonItem *rightChevron;
@property (nonatomic, strong) UIBarButtonItem *leftChevron;
@property (nonatomic, assign) BOOL nextShowKeyboardNotificationForSelf;
@property (nonatomic, assign) BOOL nextHideKeyboardNotificationForSelf;
@property (nonatomic, assign) BOOL layoutSubviewsHasBeenCalled;
@property (assign) BOOL isAnimating;
@end
@implementation STATextView
// reference: http://stackoverflow.com/a/20192857/347339
- (void)setShowBackForwardToolbar:(BOOL)showBackForwardToolbar {
_showBackForwardToolbar = showBackForwardToolbar;
if (_showBackForwardToolbar) {
UIToolbar *keyboardDoneButtonView = [UIToolbar new];
[keyboardDoneButtonView sizeToFit];
self.leftChevron = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:105
target:self
action:@selector(prevClicked:)];
self.leftChevron.accessibilityLabel = @"Previous";
[self updateEnabledStatusForBackChevron];
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
fixedSpace.width = 25.0;
self.rightChevron = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:106
target:self
action:@selector(nextClicked:)];
self.rightChevron.accessibilityLabel = @"Next";
[self updateEnabledStatusForForwardChevron];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone target:self
action:@selector(doneClicked:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
[keyboardDoneButtonView setItems:@[self.leftChevron, fixedSpace, self.rightChevron, flexibleSpace, doneButton]];
self.inputAccessoryView = keyboardDoneButtonView;
} else {
self.inputAccessoryView = nil;
}
}
- (void)prevClicked:(id)sender {
[self resignFirstResponder];
[self.prevControl becomeFirstResponder];
}
- (void)nextClicked:(id)sender {
[self resignFirstResponder];
[self.nextControl becomeFirstResponder];
}
- (CGSize)sizeForText:(NSString *)text {
//TODO: override text inset in order to update appropriately
//TODO: compensate for default text being more than one line long
// TODO: look at typing attributes
NSDictionary *attributes = nil;
if (self.font) {
// scan through nsttributed text and acount for characters with nsattributed font
attributes = @{NSFontAttributeName : self.font};
}
UIEdgeInsets textContainerInset = self.textContainerInset;
UIEdgeInsets contentInset = self.contentInset;
UIEdgeInsets totalInsets = UIEdgeInsetsMake(textContainerInset.top + contentInset.top,
textContainerInset.left + contentInset.left,
textContainerInset.bottom + contentInset.bottom,
textContainerInset.right + contentInset.right);
CGSize boundingBox = [text boundingRectWithSize:CGSizeMake(self.bounds.size.width - (totalInsets.left + totalInsets.right), 170)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes context:nil].size;
CGSize size = CGSizeMake(ceil(boundingBox.width + (totalInsets.left + totalInsets.right)), ceil(boundingBox.height + totalInsets.top + totalInsets.bottom));
return size;
}
#pragma mark Helpers
- (void)updateEnabledStatusForForwardChevron {
self.rightChevron.enabled = (BOOL)(self.nextControl);
}
- (void)updateEnabledStatusForBackChevron {
self.leftChevron.enabled = (BOOL)(self.prevControl);
}
- (void)doneClicked:(id)sender {
[self endEditing:YES];
}
#pragma mark - Subclassed Methods
- (void)layoutSubviews {
STALog(@"%s", __PRETTY_FUNCTION__);
[super layoutSubviews];
if (self.autoDeterminesHeight && !self.layoutSubviewsHasBeenCalled) {
CGSize size = [self sizeForText:@"A"]; // placeholder text
CGRect newFrame = self.frame;
newFrame.size.height = size.height;
self.frame = newFrame;
}
self.layoutSubviewsHasBeenCalled = YES;
}
- (void)textChanged:(NSNotification *)notification {
STALog(@"%s", __PRETTY_FUNCTION__);
}
- (void)textViewBeganEditing:(NSNotification *)notification {
self.isEditing = YES;
STALog(@"%s", __PRETTY_FUNCTION__);
}
- (void)textViewStoppedEditing:(NSNotification *)notification {
self.isEditing = NO;
STALog(@"%s", __PRETTY_FUNCTION__);
}
- (void)keyboardWillShow:(NSNotification *)notification {
STALog(@"%s", __PRETTY_FUNCTION__);
}
- (void)keyboardWillHide:(NSNotification *)notification {
STALog(@"%s", __PRETTY_FUNCTION__);
}
- (BOOL)becomeFirstResponder {
STALog(@"%s", __PRETTY_FUNCTION__);
[super becomeFirstResponder];
return YES;
}
- (BOOL)resignFirstResponder {
STALog(@"%s", __PRETTY_FUNCTION__);
[super resignFirstResponder];
return YES;
}
- (void)initInternal {
STALog(@"%s", __PRETTY_FUNCTION__);
[super initInternal];
_initialYPosition = self.frame.origin.y;
self.expandsUpward = NO;
self.animatesToTopOfKeyboard = NO;
self.nextHideKeyboardNotificationForSelf = NO;
self.nextShowKeyboardNotificationForSelf = NO;
self.isAnimating = NO;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
STALog(@"%s", __PRETTY_FUNCTION__);
self.nextShowKeyboardNotificationForSelf = YES;
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
STALog(@"%s", __PRETTY_FUNCTION__);
self.nextHideKeyboardNotificationForSelf = YES;
return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
STALog(@"%s", __PRETTY_FUNCTION__);
NSString *newText = [self.text stringByReplacingCharactersInRange:range withString:text];
if (!self.autoDeterminesHeight) {
return YES;
}
CGSize size = [self sizeForText:newText];
__weak STATextView *weakSelf = self;
[UIView animateWithDuration:0.1 animations:^{
CGRect newTextViewFrame = weakSelf.frame;
if (self.expandsUpward) {
CGFloat heightDelta = newTextViewFrame.size.height - size.height;
newTextViewFrame.origin.y += heightDelta;
}
newTextViewFrame.size.height = size.height;
weakSelf.frame = newTextViewFrame;
} completion:^(BOOL finished) {
if (finished) {
CGRect newTextViewFrame = weakSelf.frame;
if (self.expandsUpward) {
CGFloat heightDelta = newTextViewFrame.size.height - size.height;
newTextViewFrame.origin.y += heightDelta;
}
newTextViewFrame.size.height = size.height;
weakSelf.frame = newTextViewFrame;
}
}];
return YES;
}
@end