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
/
WMCommentBar.m
136 lines (100 loc) · 3.58 KB
/
WMCommentBar.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
//
// WMCommentBar.m
// WhiteMochaUP
//
// Created by John Liedtke on 6/14/14.
//
//
#import "WMCommentBar.h"
#import "WMCommentTextViewBar.h"
@interface WMCommentBar ()
@property (strong, nonatomic) IBOutlet UIButton *postButton;
@end
@implementation WMCommentBar
NSString * const COMMENT_PLACEHOLDER = @"Add a comment...";
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
self.textView.textContainer.lineFragmentPadding = 0;
self.textView.textContainerInset = UIEdgeInsetsZero;
self.commentTextViewBar = [[[NSBundle mainBundle] loadNibNamed:@"WMCommentTextViewBar" owner:self options:nil] objectAtIndex:0];
self.textView.inputAccessoryView = self.commentTextViewBar;
_commentTextViewBar.delegate = self;
self.textView.delegate = self;
if ([self.textView.text isEqualToString:@""]) {
self.textView.text = COMMENT_PLACEHOLDER;
self.textView.textColor = [UIColor lightGrayColor]; //optional
}
// 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;
self.textView.textContainer.lineFragmentPadding = 0;
self.textView.textContainerInset = UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0);
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 activatePostButton];
}
- (void)textViewBarFinishedEditing:(NSString *)text
{
[self.textView setText:text];
[self activatePostButton];
if ([self.textView.text isEqualToString:@""] || [self.textView.text isEqualToString:COMMENT_PLACEHOLDER]) {
self.textView.text = COMMENT_PLACEHOLDER;
self.textView.textColor = [UIColor lightGrayColor]; //optional
} else {
[self.textView setTextColor:[UIColor blackColor]];
}
[self.commentTextViewBar endEditing:YES];
}
- (IBAction)textViewActivated:(id)sender
{
[self.textView becomeFirstResponder];
}
- (void)postButtonPressed:(NSString *)comment
{
[self activatePostButton];
if (self.commentTextViewBar.textView.text.length > 0 && ![self.commentTextViewBar.textView.text isEqualToString:COMMENT_PLACEHOLDER]) {
[self.commentTextViewBar.textView setText:@""];
[self.textView setText:@""];
[self.delegate postButtonPressed:comment];
}
}
- (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];
}
}
- (IBAction)postButton:(id)sender
{
[self postButtonPressed:self.commentTextViewBar.textView.text];
// [self.delegate postButtonPressed:self.commentTextViewBar.textView.text];
}
- (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