TextInputAlertView.m
5.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
//
// 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;
@end
@implementation TextInputAlertView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHexA(0x161E38, .6), DSClearColor, DSWhite);
[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];
}
#pragma mark - private
- (void)cancelAction {
[self.textView endEditing:YES];
}
- (void)replyAction {
// 1、调用回复接口
// 2、隐藏输入框
[self.textView endEditing:YES];
}
#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, DarkColor, 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(SmallTextColor, DSWhite, 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, DSWhite, 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) bgColor:BrandColor];
[_replyBtn cornerRadius:13];
[_replyBtn addTarget:self action:@selector(replyAction) forControlEvents:UIControlEventTouchUpInside];
}
return _replyBtn;
}
@end