This repository has been archived by the owner on Mar 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WMCommentTextViewBar.m
150 lines (114 loc) · 3.74 KB
/
WMCommentTextViewBar.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
//
// WMCommentTextViewBar.m
// WhiteMochaUP
//
// Created by John Liedtke on 6/14/14.
//
//
#import "WMCommentTextViewBar.h"
#import <QuartzCore/QuartzCore.h>
#import "WMCommentBar.h"
@interface WMCommentTextViewBar ()
@property (strong, nonatomic) IBOutlet UIButton *postButton;
@end
@implementation WMCommentTextViewBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)awakeFromNib
{
self.textView.textContainer.lineFragmentPadding = 0;
self.textView.textContainerInset = UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0);
// Border
[self.textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[self.textView.layer setBorderWidth:1.0];
self.textView.layer.cornerRadius = 5;
self.textView.clipsToBounds = YES;
//UIColor *borderColor = [[UIColor alloc] initWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]
CGSize sizeThatShouldFitTheContent = [self.textView sizeThatFits:self.textView.frame.size];
CGRect savedFrame = self.frame;
savedFrame.size = sizeThatShouldFitTheContent;
//self.frame = savedFrame;
self.textView.delegate = self;
// Add a bottomBorder.
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, 0.0f, self.frame.size.width, 1.0f);
bottomBorder.backgroundColor = [UIColor colorWithWhite:0.8f
alpha:.8f].CGColor;
[self.layer addSublayer:bottomBorder];
[self updateUI];
}
const static int MAX_HEIGHT = 100;
-(void)textViewDidChange:(UITextView *)textView
{
[self updateUI];
}
- (void)updateUI
{
CGSize sizeThatShouldFitTheContent = [self.textView sizeThatFits:self.textView.frame.size];
CGRect savedFrame = self.frame;
savedFrame.size = sizeThatShouldFitTheContent;
savedFrame.size.width = 320;
savedFrame.size.height +=13;
if (savedFrame.size.height > MAX_HEIGHT) savedFrame.size.height = MAX_HEIGHT;
self.frame = savedFrame;
[self activatePostButton];
[self setNeedsDisplay];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:COMMENT_PLACEHOLDER]) {
textView.text = @"";
textView.textColor = [UIColor blackColor]; //optional
}
[self updateUI];
[self.textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@""]) {
textView.text = COMMENT_PLACEHOLDER;
textView.textColor = [UIColor lightGrayColor]; //optional
}
[self.textView resignFirstResponder];
[self.delegate textViewBarFinishedEditing:self.textView.text];
}
- (IBAction)postButtonPressed:(id)sender
{
if (self.textView.text.length > 0) {
[self.delegate postButtonPressed:self.textView.text];
[self.textView setText:@""];
}
}
- (void)activatePostButton
{
if (self.textView.text.length == 0 || [self.textView.text isEqualToString:COMMENT_PLACEHOLDER]) {
[_postButton setEnabled:NO];
[_postButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
} else {
[_postButton setEnabled:YES];
[_postButton setTitleColor:[UIColor colorWithRed:134.0/255.0 green:92.0/255.0 blue:168.0/255.0 alpha:1.0] forState:UIControlStateNormal];
}
}
- (void)hideKeyBoard
{
NSLog(@"trying to hide keybardr");
[self.textView resignFirstResponder];
}
- (void)dealloc
{
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end