TextInputAlertView.m
6.3 KB
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
//
// TextInputAlertView.m
// DreamSleep
//
// Created by peter on 2022/9/26.
//
#import "TextInputAlertView.h"
#import "MKPPlaceholderTextView.h"
#import <IQKeyboardManager/IQKeyboardManager.h>
@interface TextInputAlertView ()
@property (nonatomic, strong) UIView *alertView;
@property (nonatomic, strong) MKPPlaceholderTextView *textView;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *replyBtn;
@property (nonatomic, assign) CGFloat duration;
@property (nonatomic, copy) NSString *commentUserFlag;
@end
@implementation TextInputAlertView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = ColorFromHexA(0x161E38, .6);
[self addSubview:self.alertView];
[self.alertView addSubview:self.cancelBtn];
[self.alertView addSubview:self.replyBtn];
[self.alertView addSubview:self.textView];
[self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.alertView).offset(25);
make.top.equalTo(self.alertView).offset(18);
}];
[self.replyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.alertView).offset(-15);
make.centerY.equalTo(self.cancelBtn);
make.size.mas_equalTo(CGSizeMake(44, 26));
}];
[self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.alertView).offset(15);
make.right.equalTo(self.alertView).offset(-15);
make.top.equalTo(self.alertView).offset(53);
make.bottom.equalTo(self.alertView);
}];
[[IQKeyboardManager sharedManager] setEnable:NO];
[[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
[[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:NO];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
}
return self;
}
- (void)keyboardWillAppear:(NSNotification *)noti {
NSDictionary *info = [noti userInfo];
NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
self.duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGSize keyboardSize = [value CGRectValue].size;
[UIView animateWithDuration:self.duration animations:^{
self.alertView.y = kScreenHeight - self.alertView.height - keyboardSize.height;
}];
}
- (void)keyboardWillDisappear:(NSNotification *)noti {
[UIView animateWithDuration:self.duration animations:^{
self.alertView.y = kScreenHeight;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
- (void)dealloc {
[[IQKeyboardManager sharedManager] setEnable:YES];
[[IQKeyboardManager sharedManager] setEnableAutoToolbar:YES];
[[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark - public
- (void)showTextInputAlertView {
[DSKeyWindow addSubview:self];
[self.textView becomeFirstResponder];
self.replyBtn.tag = 1;
[self.replyBtn setTitle:@"发布" forState:UIControlStateNormal];
}
- (void)showReplyTextAlertWithCommentUser:(NSString *)commentUser {
[self showTextInputAlertView];
self.replyBtn.tag = 2;
[self.replyBtn setTitle:@"回复" forState:UIControlStateNormal];
self.commentUserFlag = [NSString stringWithFormat:@"@%@,", commentUser];
self.textView.text = self.commentUserFlag;
}
- (void)dismiss {
[self cancelAction];
}
#pragma mark - private
- (void)cancelAction {
[self.textView endEditing:YES];
self.textView.text = @"";
}
- (void)replyAction:(UIButton *)sender {
NSString *content = self.textView.text;
if (sender.tag == 2) { // 回复
content = [content componentsSeparatedByString:self.commentUserFlag].lastObject;
}
NSString *triStr = [NSString trimString:content];
if (triStr.length == 0) {
[DSProgressHUD showToast:@"请输入评论内容"];
return;
}
if (self.tapFinishBlock) {
self.tapFinishBlock(sender.tag, content);
}
}
#pragma mark - lazy
- (UIView *)alertView {
if (!_alertView) {
_alertView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 201)];
[_alertView setCornerRadiusRect:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadius:24];
_alertView.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, AlertDarkColor, DSWhite);
}
return _alertView;
}
- (MKPPlaceholderTextView *)textView {
if (!_textView) {
_textView = [MKPPlaceholderTextView new];
_textView.font = SysFont(14);
[_textView cornerRadius:12];
_textView.textContainerInset = UIEdgeInsetsMake(15, 8, 33, 8);
_textView.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, ColorFromHexA(0xFFFFFF, .5), DSWhite);
_textView.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0xF0F0F0), CornerViewDarkColor, DSWhite);
}
return _textView;
}
- (UIButton *)cancelBtn {
if (!_cancelBtn) {
_cancelBtn = [UIButton btnWithTitle:@"取消" font:SysFont(14)];
[_cancelBtn dk_setTitleColorPicker:DKColorPickerWithColors(SubTitleColor, ColorFromHexA(0xFFFFFF, .5), DSWhite) forState:UIControlStateNormal];
[_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
}
return _cancelBtn;
}
- (UIButton *)replyBtn {
if (!_replyBtn) {
_replyBtn = [UIButton btnWithTitle:@"发布" titleColor:DSWhite font:SysFont(12)];
_replyBtn.dk_backgroundColorPicker = DKColorPickerWithColors(BrandColor, SubNaviDarkColor, DSWhite);
[_replyBtn cornerRadius:13];
[_replyBtn addTarget:self action:@selector(replyAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _replyBtn;
}
@end