#define kTextLen 200
@interface xxxView()<UITextViewDelegate>
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) PlaceholderTextView *textView;
@property (nonatomic, strong) UIButton* cancelBt;
@property (nonatomic, strong) UIButton* sendBt;
@property (nonatomic, strong) UILabel *countLab;
@end
@implementation xxxView
-(instancetype)initWithFrame:(CGRect)frame image:(UIImage*)aImage
{
CGRect frame1 = [UIScreen mainScreen].bounds;
self = [super initWithFrame:frame1];
if (self)
{
UIImageView* imageView =[[UIImageView alloc] initWithFrame:frame1];
[imageView setImage:[aImage applyBlurWithRadius:5 tintColor:[UIColor colorWithWhite:1 alpha:0.2] saturationDeltaFactor:1.8 maskImage:nil]];
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTap)];
[imageView addGestureRecognizer:tap1];
[self addSubview:imageView];
[self addTarget:self action:@selector(controlPressed) forControlEvents:UIControlEventTouchUpInside];
UIView *bkView = [[UIView alloc] init];
CGFloat h1 = [OtherUtil scaleMessageHeight:130];//相对坐标适配
bkView.frame = CGRectMake(0, frame1.size.height - h1, kScreenWidth, h1);
bkView.backgroundColor = RGBCOLOR(235, 235, 235);
CGRect cancelRect = CGRectMake(8, 13, 46, 21);
cancelRect = [OtherUtil scaleMessageRect:cancelRect];
UIButton* cancelBt = [UIButton buttonWithType:UIButtonTypeCustom];
cancelBt.frame = cancelRect;
[cancelBt setTitle:@"取消" forState:UIControlStateNormal];
[cancelBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
cancelBt.backgroundColor = [UIColor clearColor];
[cancelBt addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
[bkView addSubview:cancelBt];
CGRect labRect = CGRectMake(131, 13, 58, 21);
labRect = [OtherUtil scaleMessageRect:labRect];
UILabel* lab = [[UILabel alloc] initWithFrame:labRect];
lab.text = @"写回复";
lab.textAlignment = NSTextAlignmentCenter;
lab.backgroundColor = [UIColor clearColor];
[bkView addSubview:lab];
CGRect sendRect = CGRectMake(265, 13, 46, 21);
sendRect = [OtherUtil scaleMessageRect:sendRect];
_sendBt = [UIButton buttonWithType:UIButtonTypeCustom];
_sendBt.enabled = NO;
_sendBt.frame = sendRect;
[_sendBt setTitle:@"发送" forState:UIControlStateNormal];
[_sendBt setTitleColor:RGBCOLOR(143, 143, 143) forState:UIControlStateNormal];
_sendBt.backgroundColor = [UIColor clearColor];
[_sendBt addTarget:self action:@selector(sendClick) forControlEvents:UIControlEventTouchUpInside];
[bkView addSubview:_sendBt];
CGRect textRect = CGRectMake(8, 42, 304, 68);
textRect = [OtherUtil scaleMessageRect:textRect];
self.textView = [[PlaceholderTextView alloc] initWithFrame:textRect];
self.textView.delegate = self;
[self.textView setPlaceholder:@"回复留言…"];
[self.textView setMaxlen:kTextLen];
_textView.layoutManager.allowsNonContiguousLayout = NO;
_textView.returnKeyType = UIReturnKeyDefault;
_textView.font = [UIFont systemFontOfSize:17];
__weak typeof(self) weakSelf = self;
_textView.textChangeBlock = ^(){
[weakSelf setCountLab];
};
[bkView addSubview:self.textView];
_countLab = [[UILabel alloc] init];
_countLab.textColor = RGBCOLOR(153, 153, 153);
_countLab.textAlignment = NSTextAlignmentRight;
_countLab.font = [UIFont systemFontOfSize:14];
CGFloat w = [OtherUtil scaleWidth:90];
CGFloat h = [OtherUtil scaleHeight:20];
_countLab.frame = CGRectMake(kScreenWidth-100, _textView.bottom, w, h);
[bkView addSubview:_countLab];
[self setCountLab];
[self addSubview:bkView];
self.contentView = bkView;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldEditChanged:) name:UITextViewTextDidChangeNotification object:nil];
}
return self;
}
#pragma mark KeyBoard
-(void) keyboardWillHide:(NSNotification*) notification
{
NSDictionary *userInfo = notification.userInfo;
CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
self.contentView.bottom = keyboardF.origin.y;
[UIView commitAnimations];
}
-(void) keyboardWillChangeFrame:(NSNotification*) notification
{
NSDictionary *userInfo = notification.userInfo;
CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
self.contentView.bottom = keyboardF.origin.y;
[UIView commitAnimations];
}
- (void)setCountLab
{
int strLen = (int)_textView.text.length;
_countLab.text = [NSString stringWithFormat:@"%d/%d",strLen,kTextLen];
}
-(void)cancelClick
{
[self controlPressed];
}
-(void)sendClick
{
_textView.text = [_textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (_textView.text.length == 0)
{
[AlertUtil showHudViewWithMessage:@"请输入回复..." hideAfterSecond:1];
return;
}
if ([_delegate respondsToSelector:@selector(rspSender:)])
{
_cardModel.rspContentStr = [_textView.text copy];
[_delegate rspSender:_cardModel];
}
[self controlPressed];
}
- (void)__show
{
UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview:self];
self.alpha = 1.0f;
[_textView becomeFirstResponder];
}
- (void)__hide
{
[UIView animateWithDuration:0.25f
animations:^{
self.alpha = 0.0f;
}
completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
- (void)show
{
[self __show];
}
- (void)imageTap
{
[self controlPressed];
}
- (void)controlPressed
{
[_textView resignFirstResponder];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self __hide];
});
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
return YES;
}
-(void)textFieldEditChanged:(NSNotification *)obj
{
UITextView *textView = obj.object;
if (textView.text.length)
{
[_sendBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
_sendBt.enabled = YES;
}
else
{
[_sendBt setTitleColor:RGBCOLOR(143, 143, 143) forState:UIControlStateNormal];
_sendBt.enabled = NO;
}
[OtherUtil limitInputText:textView ByMaxLength:kTextLen];
}
@end