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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// TAPCustomGrowingTextView.m
// TapTalk
//
// Created by Dominic Vedericho on 20/12/18.
// Copyright © 2018 Moselo. All rights reserved.
//
#import "TAPCustomGrowingTextView.h"
@interface TAPCustomGrowingTextView () <UITextViewDelegate>
@property (strong, nonatomic) UILabel *placeholderLabel;
- (void)initialSetup;
- (void)checkHeight;
@end
@implementation TAPCustomGrowingTextView
#pragma mark - Lifecycle
- (id)init {
self = [super init];
if (self) {
[self initialSetup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initialSetup];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initialSetup];
}
return self;
}
#pragma mark - Custom Method
- (void)initialSetup {
self.backgroundColor = [UIColor clearColor];
//For iOS 7+ - Handle jumping text
NSString *requiredSystemVersion = @"7.0";
NSString *currentSystemVersion = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currentSystemVersion compare:requiredSystemVersion options:NSNumericSearch] != NSOrderedAscending);
if (osVersionSupported) {
[_textView removeFromSuperview];
_textView = nil;
NSTextStorage *textStorage = [[NSTextStorage alloc] init];
NSLayoutManager *layoutManager = [NSLayoutManager new];
[textStorage addLayoutManager:layoutManager];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.bounds.size];
[layoutManager addTextContainer:textContainer];
UIFont *textViewFont = [[TAPStyleManager sharedManager] getComponentFontForType:TAPComponentFontMediaPreviewCaption];
UIColor *textViewColor = [[TAPStyleManager sharedManager] getTextColorForType:TAPTextColorMediaPreviewCaption];
_textView = [[TAPCustomTextView alloc] initWithFrame:self.bounds textContainer:textContainer];
self.textView.delegate = self;
self.textView.textContainer.lineFragmentPadding = 0;
self.textView.font = textViewFont;
self.textView.textColor = textViewColor;
self.textView.backgroundColor = [UIColor clearColor];
self.textView.showsVerticalScrollIndicator = NO;
self.textView.showsHorizontalScrollIndicator = NO;
self.textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.textView.autocorrectionType = UITextAutocorrectionTypeDefault;
self.textView.spellCheckingType = UITextSpellCheckingTypeDefault;
self.textView.keyboardType = UIKeyboardTypeDefault;
self.textView.keyboardAppearance = UIKeyboardAppearanceDefault;
self.textView.returnKeyType = UIReturnKeyDefault;
self.textView.inputView.autoresizingMask = YES;
[self addSubview:self.textView];
}
self.textView.textContainerInset = UIEdgeInsetsZero;
_placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.textView.frame) + self.textView.textContainerInset.left, CGRectGetMinY(self.textView.frame), CGRectGetWidth(self.textView.frame), CGRectGetHeight(self.textView.frame))];
self.placeholderLabel.backgroundColor = [UIColor clearColor];
[self addSubview:self.placeholderLabel];
if (self.minimumHeight == 0.0f) {
self.minimumHeight = 22.0f;
}
if (self.maximumHeight == 0.0f) {
self.maximumHeight = 60.0f;
}
self.characterCountLimit = 0;
}
#pragma mark - Delegate
#pragma mark UITextView
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
if (self.characterCountLimit != 0 && [newString length] > self.characterCountLimit) {
return NO;
}
_text = newString;
if ([newString isEqualToString:@""]) {
self.placeholderLabel.alpha = 1.0f;
if ([self.delegate respondsToSelector:@selector(customGrowingTextViewDidStopTyping:)]) {
[self.delegate customGrowingTextViewDidStopTyping:self];
}
}
else {
self.placeholderLabel.alpha = 0.0f;
if ([self.delegate respondsToSelector:@selector(customGrowingTextViewDidStartTyping:)]) {
[self.delegate customGrowingTextViewDidStartTyping:self];
}
}
if ([self.delegate respondsToSelector:@selector(customGrowingTextView:shouldChangeTextInRange:replacementText:)]) {
BOOL result = [self.delegate customGrowingTextView:textView shouldChangeTextInRange:range replacementText:text];
return result;
}
return YES;
}
- (void)textViewDidChange:(UITextView *)textView {
[self checkHeight];
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
self.placeholderLabel.alpha = 0.0f;
if ([self.delegate respondsToSelector:@selector(customGrowingTextViewDidBeginEditing:)]) {
[self.delegate customGrowingTextViewDidBeginEditing:self];
}
}
- (void)textViewDidEndEditing:(UITextView *)textView {
if ([self.delegate respondsToSelector:@selector(customGrowingTextViewDidEndEditing:)]) {
[self.delegate customGrowingTextViewDidEndEditing:self];
}
}
#pragma mark - Custom Method
- (void)setFont:(UIFont *)font {
_font = font;
self.textView.font = font;
self.placeholderLabel.font = font;
}
- (void)setTextColor:(UIColor *)color {
self.textView.textColor = color;
}
- (void)setPlaceholderColor:(UIColor *)color {
self.placeholderLabel.textColor = color;
}
- (void)setText:(NSString *)text {
_text = text;
self.textView.text = text;
if ([self.text isEqualToString:@""]) {
self.placeholderLabel.alpha = 1.0f;
if ([self.delegate respondsToSelector:@selector(customGrowingTextViewDidStopTyping:)]) {
[self.delegate customGrowingTextViewDidStopTyping:self];
}
}
else {
self.placeholderLabel.alpha = 0.0f;
if ([self.delegate respondsToSelector:@selector(customGrowingTextViewDidStartTyping:)]) {
[self.delegate customGrowingTextViewDidStartTyping:self];
}
}
[self checkHeight];
}
- (void)setInitialText:(NSString *)text {
_text = text;
self.textView.text = text;
if ([self.text isEqualToString:@""]) {
self.placeholderLabel.alpha = 1.0f;
}
else {
self.placeholderLabel.alpha = 0.0f;
}
[self checkHeight];
}
- (void)checkHeight {
CGSize contentSize = [self.textView sizeThatFits:CGSizeMake(CGRectGetWidth(self.frame), MAXFLOAT + self.textView.textContainerInset.top + self.textView.textContainerInset.bottom)];
self.textView.textContainer.size = CGSizeMake(self.textView.textContainer.size.width, contentSize.height);
if (contentSize.height < self.minimumHeight) {
contentSize.height = self.minimumHeight;
}
if (contentSize.height > self.maximumHeight) {
contentSize.height = self.maximumHeight;
}
self.textView.frame = CGRectMake(CGRectGetMinX(self.textView.frame), CGRectGetMinY(self.textView.frame), CGRectGetWidth(self.textView.frame), contentSize.height);
self.placeholderLabel.frame = CGRectMake(CGRectGetMinX(self.textView.frame) + self.textView.textContainerInset.left, CGRectGetMinY(self.textView.frame), CGRectGetWidth(self.textView.frame), CGRectGetHeight(self.textView.frame));
if (contentSize.height != CGRectGetHeight(self.frame)) {
if ([self.delegate respondsToSelector:@selector(customGrowingTextView:shouldChangeHeight:)]) {
[self.delegate customGrowingTextView:self shouldChangeHeight:contentSize.height];
}
}
}
- (void)becameFirstResponder {
[self.textView becomeFirstResponder];
// [self performSelector:@selector(setTextViewFirstResponder) withObject:nil afterDelay:0.2f];
}
- (void)resignFirstResponder {
[self.textView resignFirstResponder];
}
- (void)resignFirstResponderWithoutAnimation {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.0];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[self.textView resignFirstResponder];
[UIView commitAnimations];
}
- (BOOL)isFirstResponder {
return [self.textView isFirstResponder];
}
- (void)setInputView:(UIView *)inputView {
self.textView.inputView = inputView;
[self.textView reloadInputViews];
}
- (void)selectAll:(id)sender {
[self.textView selectAll:sender];
}
- (void)setPlaceholderText:(NSString *)text {
self.placeholderLabel.text = text;
}
- (void)setCharCountLimit:(NSInteger)limit {
_characterCountLimit = limit;
}
@end