-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComposeCommentView.m
215 lines (116 loc) · 5.33 KB
/
ComposeCommentView.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
//
// ComposeCommentView.m
// Blocstagram_V3
//
// Created by Diego Aguirre on 6/10/15.
// Copyright (c) 2015 Diego Aguirre. All rights reserved.
//
#import "ComposeCommentView.h"
@interface ComposeCommentView () <UITextViewDelegate>
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIDynamicAnimator *dynamicBounce;
@end
@implementation ComposeCommentView
- (id) initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.textView = [UITextView new];
self.textView.delegate = self;
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.button setAttributedTitle:[self commentAttributedString] forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(commentButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.textView];
[self.textView addSubview:self.button];
}
return self;
}
- (NSAttributedString *) commentAttributedString{
NSString *baseString = NSLocalizedString(@"COMMENT", @"comment button text");
NSRange range = [baseString rangeOfString:baseString];
NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc]initWithString:baseString];
[commentString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:10] range:range];
[commentString addAttribute:NSKernAttributeName value:@1.3 range:range];
[commentString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.933 green:0.933 blue:0.933 alpha:1] range:range];
return commentString;
}
- (void) layoutSubviews {
[super layoutSubviews];
self.textView.frame = self.bounds;
if (self.isWritingComment) {
self.textView.backgroundColor = [UIColor colorWithRed:0.933 green:0.933 blue:0.933 alpha:1];/*#eeeeee*/
self.button.backgroundColor = [UIColor colorWithRed:0.345 green:0.318 blue:0.424 alpha:1]; /*#58516c*/
[UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.0 initialSpringVelocity:0.008 options:UIViewAnimationOptionCurveLinear animations:^{
//your animations go here
CGFloat buttonX = CGRectGetWidth(self.bounds) - CGRectGetWidth(self.button.frame) - 20;
self.button.frame = CGRectMake(buttonX, 10, 80, 20);
UIDynamicItemBehavior *elasticityBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.button]];
elasticityBehavior.elasticity = 1.05f;
[self.dynamicBounce addBehavior:elasticityBehavior];
}
completion:nil];
} else {
self.textView.backgroundColor = [UIColor colorWithRed:0.898 green:0.898 blue:0.898 alpha:1];
self.button.backgroundColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1];
self.button.frame = CGRectMake(10, 10, 80, 20);
}
CGSize buttonSize = self.button.frame.size;
buttonSize.height += 20;
buttonSize.width += 20;
CGFloat blockX = CGRectGetWidth(self.textView.bounds) - buttonSize.width;
CGRect areaToBlockText = CGRectMake(blockX, 0, buttonSize.width, buttonSize.height);
UIBezierPath *buttonPath = [UIBezierPath bezierPathWithRect:areaToBlockText];
self.textView.textContainer.exclusionPaths = @[buttonPath];
}
- (void)stopComposingComment{
[self.textView resignFirstResponder];
}
#pragma mark - Setters & getters
- (void) setIsWritingComment:(BOOL)isWritingComment{
[self setIsWritingComment:isWritingComment animated:NO];
}
- (void) setIsWritingComment:(BOOL)isWritingComment animated:(BOOL) animated{
_isWritingComment = isWritingComment;
if (animated) {
[UIView animateWithDuration:0.2 animations:^{
[self layoutSubviews];
}];
}else {
[self layoutSubviews];
}
}
- (void) setText:(NSString *)text{
_text = text;
self.textView.text = text;
self.textView.userInteractionEnabled = YES;
self.isWritingComment = text.length > 0;
}
#pragma mark - Button Target
- (void)commentButtonPressed:(UIButton *)sender {
if (self.isWritingComment) {
[self.textView resignFirstResponder];
self.textView.userInteractionEnabled = NO;
[self.delegate commentViewDidPressCommentButton:self];
}else {
[self setIsWritingComment:YES animated:YES];
[self.textView becomeFirstResponder];
}
}
#pragma mark - UITextViewDelegate
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
[self setIsWritingComment:YES animated:YES];
[self.delegate commentViewWillStartEditing:self];
return YES;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
NSString *newText = [textView.text stringByReplacingCharactersInRange:range withString:text];
[self.delegate commentView:self textDidChange:newText];
return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
BOOL hasComment = (textView.text.length > 0);
[self setIsWritingComment:hasComment animated:YES];
return YES;
}
@end