// // commodityController.m // ThePaperBase // // Created by Huixin on 15/10/22. // Copyright © 2015年 scar1900. All rights reserved. // #import "commodityController.h" #import "AsyncImageView.h" #import "messageController.h" @interface commodityController () { CGFloat descHeight; CGFloat keyboardHeight; CGFloat contentOffsetY; } @property(nonatomic, strong)UIScrollView *commodityView; @property(nonatomic, strong)AsyncImageView *commodityImg; @property(nonatomic, strong)UILabel *commodityNameLabel; @property(nonatomic, strong)UILabel *numLabel; @property(nonatomic, strong)UILabel *seashellLabel; @property(nonatomic, strong)UIView *line; @property(nonatomic, strong)UILabel *descLabel; @property(nonatomic, strong)UIImageView *nameIcon; @property(nonatomic, strong)UITextField *nameField; @property(nonatomic, strong)UIView *nameLine; @property(nonatomic, strong)UIImageView *phoneIcon; @property(nonatomic, strong)UITextField *phoneField; @property(nonatomic, strong)UIView *phoneLine; @property(nonatomic, strong)UITextView *addressView; @property(nonatomic, strong)UILabel *holderLabel; @property(nonatomic, strong)UILabel *bottomLabel; @property(nonatomic, strong)TPCustomButton *btn; @property(nonatomic, strong)UIAlertView *oneOptionAlertView; @property(nonatomic, strong)UIAlertView *twoOptionsAlertView; @property(nonatomic, strong)UIAlertView *confirmAlertView; @property(nonatomic, strong)NSDictionary *dic; @end @implementation commodityController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.titleStr = @"海贝商城"; self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; [self.view addSubview:self.commodityView]; [self.commodityView addSubview:self.commodityImg]; [self.commodityView addSubview:self.commodityNameLabel]; [self.commodityView addSubview:self.numLabel]; [self.commodityView addSubview:self.seashellLabel]; [self.commodityView addSubview:self.line]; [self.commodityView addSubview:self.descLabel]; if ([self.commodity.realOrVirtual isEqualToString:@"0"]) { [self.commodityView addSubview:self.nameIcon]; [self.commodityView addSubview:self.nameField]; [self.commodityView addSubview:self.nameLine]; [self.commodityView addSubview:self.phoneIcon]; [self.commodityView addSubview:self.phoneField]; [self.commodityView addSubview:self.phoneLine]; [self.commodityView addSubview:self.addressView]; [self.commodityView addSubview:self.holderLabel]; [self.commodityView addSubview:self.bottomLabel]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)]; [self.view addGestureRecognizer:tapGesture]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil]; keyboardHeight = 0; contentOffsetY = 0; self.phoneField.text = [TPUserDefault instance].userBO.mobile; self.addressView.text = [TPUserDefault instance].userBO.address; if (self.addressView.text.length > 0) self.holderLabel.hidden = YES; else self.holderLabel.hidden = NO; } [self.view addSubview:self.btn]; [self layoutViews]; self.btn.hidden = YES; [self remoteAction]; } - (void)layoutViews { [self.commodityView makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.naviBar.bottom); make.left.equalTo(self.view.left); make.right.equalTo(self.view.right); }]; CGFloat imgWidth = rect_screen.size.width/2.0-10; [self.commodityImg makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.commodityView.top).offset(10); make.left.equalTo(self.view.left).offset(10); make.height.and.width.mas_equalTo(imgWidth); }]; CGFloat nameHeight = [self.commodityNameLabel sizeThatFits:CGSizeMake(imgWidth-10, 0)].height; [self.commodityNameLabel makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.commodityView.top).offset(10); make.left.equalTo(self.commodityImg.right).offset(10); make.right.equalTo(self.view.right).offset(-10); make.height.mas_equalTo(nameHeight); }]; CGFloat numWidth = [self.numLabel sizeThatFits:CGSizeMake(0, 20)].width; [self.numLabel makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.commodityNameLabel.bottom).offset(12); make.left.equalTo(self.commodityNameLabel.left); make.height.mas_equalTo(@20); make.width.mas_equalTo(numWidth); }]; [self.seashellLabel makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.numLabel.right).offset(5); make.bottom.equalTo(self.numLabel.bottom); make.right.equalTo(self.commodityNameLabel.right); make.height.mas_equalTo(@15); }]; [self.line makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.commodityImg.bottom).offset(10); make.left.equalTo(self.commodityImg.left); make.right.equalTo(self.commodityNameLabel.right); make.height.mas_equalTo(@0.5); }]; [self.descLabel makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.line.bottom).offset(15); make.left.equalTo(self.line.left); make.right.equalTo(self.line.right); make.height.mas_equalTo(@0); }]; if ([self.commodity.realOrVirtual isEqualToString:@"0"]) { [self.nameIcon makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.line.left); make.bottom.equalTo(self.nameField.bottom); make.width.mas_equalTo(@18.5); make.height.mas_equalTo(@19); }]; [self.nameField makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.descLabel.bottom).offset(22); make.left.equalTo(self.line.left).offset(30); make.right.equalTo(self.line.right); make.height.mas_equalTo(@19); }]; [self.nameLine makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.nameField.bottom).offset(7); make.left.equalTo(self.line.left); make.right.equalTo(self.line.right); make.height.mas_equalTo(@1); }]; [self.phoneIcon makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.line.left); make.bottom.equalTo(self.phoneField.bottom); make.width.mas_equalTo(@18); make.height.mas_equalTo(@18); }]; [self.phoneField makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.nameLine.bottom).offset(30); make.left.equalTo(self.line.left).offset(30); make.right.equalTo(self.line.right); make.height.mas_equalTo(@16); }]; [self.phoneLine makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.phoneField.bottom).offset(7); make.left.equalTo(self.line.left); make.right.equalTo(self.line.right); make.height.mas_equalTo(@1); }]; [self.addressView makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.phoneLine.bottom).offset(28); make.left.equalTo(self.line.left); make.right.equalTo(self.line.right); make.height.mas_equalTo(@92); }]; [self.holderLabel makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.addressView.top).offset(10); make.left.equalTo(self.addressView.left).offset(5); make.width.mas_equalTo(@120); make.height.mas_equalTo(@16); }]; [self.bottomLabel makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.addressView.bottom).offset(6); make.left.equalTo(self.line.left); make.right.equalTo(self.line.right); make.height.mas_equalTo(@13); make.bottom.equalTo(self.commodityView.bottom).offset(-10); }]; } [self.btn makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.commodityView.bottom); make.left.equalTo(self.view.left); make.right.equalTo(self.view.right); make.bottom.equalTo(self.view.bottom); make.height.mas_equalTo(@40); }]; } - (void)reLayoutViews { if ([self.commodity.realOrVirtual isEqualToString:@"0"]) { [self.descLabel remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.line.bottom).offset(15); make.left.equalTo(self.line.left); make.right.equalTo(self.line.right); make.height.mas_equalTo(descHeight); }]; } else { [self.descLabel remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.line.bottom).offset(15); make.left.equalTo(self.line.left); make.right.equalTo(self.line.right); make.height.mas_equalTo(descHeight); make.bottom.equalTo(self.commodityView.bottom).offset(-10); }]; } if ([self.commodity.realOrVirtual isEqualToString:@"0"]) self.commodityView.contentSize = CGSizeMake(rect_screen.size.width, rect_screen.size.width/2.0+descHeight+287.5); else self.commodityView.contentSize = CGSizeMake(rect_screen.size.width, rect_screen.size.width/2.0+descHeight+45.5); } #pragma mark - get and set method - (UIScrollView*)commodityView { if (!_commodityView) { _commodityView = [UIScrollView new]; _commodityView.backgroundColor = [UIColor clearColor]; _commodityView.showsVerticalScrollIndicator = NO; _commodityView.showsHorizontalScrollIndicator = NO; _commodityView.scrollEnabled = YES; _commodityView.directionalLockEnabled = YES; _commodityView.alwaysBounceVertical = YES; _commodityView.contentSize = CGSizeMake(rect_screen.size.width, rect_screen.size.height-self.barHeight-40); _commodityView.delegate = self; } return _commodityView; } - (AsyncImageView*)commodityImg { if (!_commodityImg) { _commodityImg = [[AsyncImageView alloc] init]; } return _commodityImg; } - (UILabel*)commodityNameLabel { if (!_commodityNameLabel) { _commodityNameLabel = [UILabel new]; _commodityNameLabel.backgroundColor = [UIColor clearColor]; _commodityNameLabel.font = appFont(TEXT_THREE_LEVELSIZE, NO); _commodityNameLabel.textColor = [UIColor colorWithHexString:TextBlack]; _commodityNameLabel.numberOfLines = 0; } return _commodityNameLabel; } - (UILabel*)numLabel { if (!_numLabel) { _numLabel = [UILabel new]; _numLabel.backgroundColor = [UIColor clearColor]; _numLabel.font = appFont(TEXT_ONE_PLUS_LEVELSIZE, NO); _numLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; } return _numLabel; } - (UILabel*)seashellLabel { if (!_seashellLabel) { _seashellLabel = [UILabel new]; _seashellLabel.backgroundColor = [UIColor clearColor]; _seashellLabel.font = appFont(TEXT_FIVE_LEVELSIZE, NO); _seashellLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; _seashellLabel.text = @"海贝"; } return _seashellLabel; } - (UIView*)line { if (!_line) { _line = [UIView new]; _line.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; } return _line; } - (UILabel*)descLabel { if (!_descLabel) { _descLabel = [UILabel new]; _descLabel.backgroundColor = [UIColor clearColor]; _descLabel.font = appFont(TEXT_FIVE_LEVELSIZE, NO); _descLabel.textColor = [UIColor colorWithHexString:TextBlack]; _descLabel.numberOfLines = 0; } return _descLabel; } - (UIImageView*)nameIcon { if (!_nameIcon) { _nameIcon = [[UIImageView alloc] init]; _nameIcon.image = Image(@"mall/nameHead.png"); } return _nameIcon; } - (UITextField*)nameField { if (!_nameField) { _nameField = [[UITextField alloc] init]; _nameField.delegate = self; _nameField.backgroundColor = [UIColor clearColor]; NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:@"姓名" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:TextLightGray]}]; _nameField.attributedPlaceholder = atrString; _nameField.textColor = [UIColor colorWithHexString:TextBlack]; _nameField.font = appFont(TEXT_FOUR_LEVELSIZE, NO); _nameField.textAlignment = NSTextAlignmentLeft; _nameField.keyboardAppearance = UIKeyboardAppearanceDefault; if ([[TPUserDefault instance].isNightMode intValue] > 0) { _nameField.keyboardAppearance = UIKeyboardAppearanceDark; } _nameField.keyboardType = UIKeyboardTypeDefault; _nameField.clearButtonMode = UITextFieldViewModeWhileEditing; [_nameField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; } return _nameField; } - (UIView*)nameLine { if (!_nameLine) { _nameLine = [[UIView alloc] init]; _nameLine.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; } return _nameLine; } - (UIImageView*)phoneIcon { if (!_phoneIcon) { _phoneIcon = [[UIImageView alloc] init]; _phoneIcon.image = Image(@"mall/phone.png"); } return _phoneIcon; } - (UITextField*)phoneField { if (!_phoneField) { _phoneField = [[UITextField alloc] init]; _phoneField.delegate = self; _phoneField.backgroundColor = [UIColor clearColor]; NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:@"手机" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:TextLightGray]}]; _phoneField.attributedPlaceholder = atrString; _phoneField.textColor = [UIColor colorWithHexString:TextBlack]; _phoneField.font = appFont(TEXT_FOUR_LEVELSIZE, NO); _phoneField.textAlignment = NSTextAlignmentLeft; _phoneField.keyboardAppearance = UIKeyboardAppearanceDefault; if ([[TPUserDefault instance].isNightMode intValue] > 0) { _phoneField.keyboardAppearance = UIKeyboardAppearanceDark; } _phoneField.keyboardType = UIKeyboardTypePhonePad; _phoneField.clearButtonMode = UITextFieldViewModeWhileEditing; [_phoneField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; } return _phoneField; } - (UIView*)phoneLine { if (!_phoneLine) { _phoneLine = [[UIView alloc] init]; _phoneLine.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; } return _phoneLine; } - (UITextView*)addressView { if (!_addressView) { _addressView = [[UITextView alloc] init]; _addressView.layer.cornerRadius = 5; _addressView.layer.borderWidth =1; _addressView.font = appFont(TEXT_FOUR_LEVELSIZE, NO); _addressView.textColor = [UIColor colorWithHexString:TextBlack]; _addressView.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor; if ([[TPUserDefault instance].isNightMode intValue] > 0) _addressView.keyboardAppearance = UIKeyboardAppearanceDark; else _addressView.keyboardAppearance = UIKeyboardAppearanceDefault; _addressView.keyboardType = UIKeyboardTypeDefault; _addressView.delegate = self; _addressView.backgroundColor = [UIColor clearColor]; } return _addressView; } - (UILabel*)holderLabel { if (!_holderLabel) { _holderLabel = [[UILabel alloc] init]; _holderLabel.text = @"请填写收货地址"; _holderLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO); _holderLabel.textColor = [UIColor colorWithHexString:TextLightGray]; _holderLabel.backgroundColor = [UIColor clearColor]; } return _holderLabel; } - (UILabel*)bottomLabel { if (!_bottomLabel) { _bottomLabel = [UILabel new]; _bottomLabel.backgroundColor = [UIColor clearColor]; _bottomLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO); _bottomLabel.textColor = [UIColor colorWithHexString:TextLightGray]; _bottomLabel.text = @"请务必准确填写您的联系方式!"; } return _bottomLabel; } - (TPCustomButton*)btn { if (!_btn) { _btn = [TPCustomButton new]; _btn.layer.cornerRadius = 0.f; [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; } return _btn; } - (void)setCommodity:(commodityBO *)commodity { _commodity = commodity; self.commodityImg.imageUrl = commodity.pic; NSString *imageID = getImageNameFromURL(commodity.pic); self.commodityImg.imageId = imageID; self.commodityNameLabel.text = commodity.name; self.numLabel.text = commodity.seashell; } #pragma mark - btn handler - (void)btnClick { if ([self.commodity.status isEqualToString:@"1"]) { //正常 self.dic = [NSDictionary new]; if ([self.commodity.realOrVirtual isEqualToString:@"0"]) { if (isBlankString(_nameField.text) || isBlankString(_phoneField.text)) { ShowTextMessage(@"姓名或手机不能为空!"); return; } else if (isBlankString(_addressView.text)) { ShowTextMessage(@"地址不能为空!"); return; } else self.dic = @{@"cid": self.commodity.cid, @"realname": self.nameField.text, @"msisdn": self.phoneField.text, @"address": self.addressView.text}; } else self.dic = @{@"cid": self.commodity.cid}; if (!_confirmAlertView) { _confirmAlertView = [[UIAlertView alloc]initWithTitle:@"" message:@"是否确认参加本次活动?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil]; } [self.confirmAlertView show]; // [Remote doJsonAction:2 requestUrl:saleURL parameter:dic delegate:self]; } } #pragma mark - remote action - (void)remoteAction { NSDictionary *dic = @{@"cid": self.commodity.cid}; // __weak typeof(self) weakSelf = self; // [Remote doJsonActionWithBlock:1 requestUrl:commodityURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { // if (success) { // weakSelf.commodity.description = responseData[@"commodity"][@"description"]; // weakSelf.commodity.isOnly = responseData[@"commodity"][@"isOnly"]; // weakSelf.commodity.status = responseData[@"commodity"][@"status"]; // weakSelf.commodity.buttonValue = responseData[@"commodity"][@"button"]; // weakSelf.commodity.pubTime = responseData[@"commodity"][@"pubTime"]; // weakSelf.descLabel.text = weakSelf.commodity.description; // [weakSelf.btn setTitle:weakSelf.commodity.buttonValue]; // [weakSelf reLayoutViews]; // weakSelf.btn.hidden = NO; // NSInteger i = [weakSelf.commodity.status integerValue]; // if (i != 1) { // weakSelf.btn.userInteractionEnabled = NO; // [weakSelf.btn setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:BUTTONDISABLEBACK]) // forState:UIControlStateNormal]; // } // } // else { // ShowTextMessage(message); // } // }]; [Remote doJsonAction:1 requestUrl:commodityURL parameter:dic delegate:self]; } #pragma mark - remote delegate - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responseData { if (actionTag == 1) { self.commodity.description = responseData[@"commodity"][@"description"]; self.commodity.isOnly = responseData[@"commodity"][@"isOnly"]; self.commodity.status = responseData[@"commodity"][@"status"]; self.commodity.buttonValue = responseData[@"commodity"][@"button"]; self.commodity.pubTime = responseData[@"commodity"][@"pubTime"]; NSAttributedString *attributedStr = getLineSpaceAttributedString(self.commodity.description, 7, self.descLabel.font); self.descLabel.attributedText = attributedStr; descHeight = heightForAttributeStringWithLabel(attributedStr, rect_screen.size.width-20, self.descLabel.font); [self.btn setTitle:self.commodity.buttonValue]; self.btn.hidden = NO; if ([self.commodity.status integerValue] != 1) { self.btn.userInteractionEnabled = NO; [self.btn setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:BUTTONDISABLEBACK]) forState:UIControlStateNormal]; } [self reLayoutViews]; } else if (actionTag == 2) { self.commodity.status = responseData[@"status"]; self.commodity.buttonValue = responseData[@"button"]; [self.btn setTitle:self.commodity.buttonValue]; //bug5797: 积分商城:抽奖和兑换后下方的按钮没有变化 if ([self.commodity.status integerValue] != 1) { self.btn.enabled = NO; } if ([self.commodity.saleMode isEqualToString:@"2"]) { //抽奖 if (_oneOptionAlertView == nil) { _oneOptionAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"message" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil]; } _oneOptionAlertView.message = responseData[@"resultMsg"]; [_oneOptionAlertView show]; } else if ([self.commodity.saleMode isEqualToString:@"1"]) { //兑换 if (_twoOptionsAlertView == nil) { _twoOptionsAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"message" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:@"马上查看", nil]; } _twoOptionsAlertView.message = responseData[@"resultMsg"]; [_twoOptionsAlertView show]; } if ([self.commodity.realOrVirtual isEqualToString:@"0"]) { [Remote doJsonAction:3 requestUrl:userInfoURL parameter:nil delegate:self]; } } else if (actionTag == 3) { //bug6026: 积分商城:填入地址提交后有两个症状 userBO *user = setJsonDicToDataModel(responseData[@"userInfo"], [userBO class]); NSString *lastLoginType = [TPUserDefault instance].userBO.loginType; user.loginType = lastLoginType; [TPUserDefault instance].userBO = user; } } - (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code { ShowTextMessage(message); } #pragma mark - uialertview delegate - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView == _twoOptionsAlertView) { if (buttonIndex == alertView.cancelButtonIndex) { return; } if (buttonIndex == 1) { messageController *vc = [messageController new]; [self.navigationController pushViewController:vc animated:YES]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [vc setCurrentIndex:1]; }); } } if (alertView == _confirmAlertView) { if (buttonIndex != 0) { [Remote doJsonAction:2 requestUrl:saleURL parameter:self.dic delegate:self]; } } } #pragma mark - textfield delegate - (void)textFieldDidChange:(UITextField *)textField { if ([textField.text isMatchedByRegex:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"]) { textField.text = disable_emoji(textField.text); } } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [self closeKeyBoard]; return YES; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if ([textField isFirstResponder]) { if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) { return NO; } } return YES; } #pragma mark - tap handler - (void)tap:(UITapGestureRecognizer*)sender { [self closeKeyBoard]; } #pragma mark - uitextview delagate - (void)textViewDidChange:(UITextView *)textView { if ([textView.text isMatchedByRegex:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"]) { textView.text = disable_emoji(textView.text); } if (textView.text.length > 0) _holderLabel.hidden = YES; else _holderLabel.hidden = NO; if (textView.text.length > 400) textView.text = [textView.text substringToIndex:400]; } - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if ([textView isFirstResponder]) { if ([[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textView textInputMode] primaryLanguage]) { return NO; } } return YES; } #pragma mark - uiscrollview delegate - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { if (!decelerate) { contentOffsetY = scrollView.contentOffset.y; } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { contentOffsetY = scrollView.contentOffset.y; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [self closeKeyBoard]; } #pragma mark - keyBoard event - (void)keyboardDidShow:(NSNotification *)notification { // contentOffsetY = self.commodityView.contentOffset.y; if ([self.nameField isFirstResponder]) { NSDictionary *userInfo = [notification userInfo]; CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; keyboardHeight = keyboardSize.height; CGFloat distance = rect_screen.size.height-self.barHeight-(CGRectGetMaxY(self.nameLine.frame)-contentOffsetY); if (keyboardHeight > distance) { [self.commodityView setContentOffset:CGPointMake(0, contentOffsetY+keyboardHeight-distance) animated:YES]; } } if ([self.phoneField isFirstResponder]) { NSDictionary *userInfo = [notification userInfo]; CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; keyboardHeight = keyboardSize.height; CGFloat distance = rect_screen.size.height-self.barHeight-(CGRectGetMaxY(self.phoneLine.frame)-contentOffsetY); if (keyboardHeight > distance) { [self.commodityView setContentOffset:CGPointMake(0, contentOffsetY+keyboardHeight-distance) animated:YES]; } } if ([self.addressView isFirstResponder]) { NSDictionary *userInfo = [notification userInfo]; CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; keyboardHeight = keyboardSize.height; CGFloat distance = rect_screen.size.height-self.barHeight-(CGRectGetMaxY(self.addressView.frame)-contentOffsetY); if (keyboardHeight > distance) { [self.commodityView setContentOffset:CGPointMake(0, contentOffsetY+keyboardHeight-distance) animated:YES]; } } } - (void)keyboardDidHide:(NSNotification *)notification { [self.commodityView setContentOffset:CGPointMake(0, contentOffsetY) animated:YES]; } - (void)closeKeyBoard { [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]; } #pragma mark - naviBar - (void)tapNaviBar:(id)sender { [self closeKeyBoard]; contentOffsetY = 0; [self.commodityView setContentOffset:CGPointMake(0, contentOffsetY) animated:YES]; } - (void)dealloc { [[NSNotificationCenter defaultCenter]removeObserver:self]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end