|
//
// leakController.m
// ThePaperBase
//
// Created by scar1900 on 15/7/29.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
#import "leakController.h"
#import "leakCollectionController.h"
#import "UIImage-Extensions.h"
@interface leakController()<UITextViewDelegate, UITextFieldDelegate, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, leakCollectionControllerDelegate> {
CGFloat keyboardHeight;
NSInteger imageUploadFlag;
}
@property(nonatomic, strong)UIScrollView *backView;
@property(nonatomic, strong)TPCustomButton *phoneBtn;
@property(nonatomic, strong)UIImageView *phoneIcon;
@property(nonatomic, strong)UILabel *phoneLabel;
@property(nonatomic, strong)UIActionSheet *phoneMenu;
@property(nonatomic, strong)UIView *containerView1;
@property(nonatomic, strong)UITextView *leakContent;
@property(nonatomic, strong)UILabel *holderLabel;
@property(nonatomic, strong)UILabel *numLabel;
@property(nonatomic, strong)UIImageView *mailIcon;
@property(nonatomic, strong)UITextField *mailTextField;
@property(nonatomic, strong)UIView *line;
@property(nonatomic, strong)UIButton *uploadBtn;
@property(nonatomic, strong)UILabel *uploadLabel;
@property(nonatomic, strong)UIActionSheet *imageMenu;
@property(nonatomic, strong)UIView *containerView2;
@property(nonatomic, strong)leakCollectionController *collectionController;
@property(nonatomic, strong)TPCustomButton *confirmBtn;
@property(nonatomic, strong)NSMutableArray *imageIDList;
@property(nonatomic, strong)NSMutableArray *imageList;
@end
@implementation leakController
- (void)viewDidLoad {
[super viewDidLoad];
[MobClick event:@"45"];
self.titleStr = @"报料";
self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
keyboardHeight = 0;
[self.view addSubview:self.backView];
[self.backView addSubview:self.phoneBtn];
[self.phoneBtn addSubview:self.containerView1];
[self.containerView1 addSubview:self.phoneIcon];
[self.containerView1 addSubview:self.phoneLabel];
[self.backView addSubview:self.leakContent];
[self.backView addSubview:self.holderLabel]; //bug5092: 【适配性】4s点击“报料”闪退
[self.backView addSubview:self.numLabel];
[self.backView addSubview:self.mailIcon];
[self.backView addSubview:self.mailTextField];
[self.backView addSubview:self.line];
[self.backView addSubview:self.containerView2];
[self.containerView2 addSubview:self.uploadBtn];
[self addChildViewController:self.collectionController];
[self.containerView2 addSubview:self.collectionController.view];
[self.collectionController didMoveToParentViewController:self];
[self.backView addSubview:self.uploadLabel];
[self.backView addSubview:self.confirmBtn];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapToDismiss:)];
[self.view addGestureRecognizer:tap];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
[self layoutViews];
}
- (void)layoutViews {
[self.backView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.naviBar.bottom);
make.left.equalTo(self.view.left);
make.right.equalTo(self.view.right);
make.bottom.equalTo(self.view.bottom);
}];
[self.phoneBtn makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.backView.top).offset(25);
make.left.equalTo(self.view.left).offset(20);
make.right.equalTo(self.view.right).offset(-20);
make.height.equalTo(self.phoneBtn.width).multipliedBy(0.15);
}];
[self.containerView1 makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.phoneBtn.center);
make.height.equalTo(self.phoneBtn.height);
make.width.mas_equalTo(@260);
}];
[self.phoneIcon makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.containerView1.left);
make.width.and.height.equalTo(self.containerView1.height).multipliedBy(0.65);
make.centerY.equalTo(self.containerView1.centerY);
}];
[self.phoneLabel makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.phoneIcon.right);
make.right.equalTo(self.containerView1.right);
make.height.mas_equalTo(@18);
make.centerY.equalTo(self.containerView1.centerY);
}];
[self.leakContent makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.phoneBtn.bottom).offset(35);
make.left.equalTo(self.view.left).offset(20);
make.right.equalTo(self.view.right).offset(-20);
make.height.equalTo(self.leakContent.width).multipliedBy(0.4);
}];
[self.holderLabel makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.leakContent.top).offset(10);
make.left.equalTo(self.leakContent.left).offset(5);
make.width.mas_equalTo(@100);
make.height.mas_equalTo(@16);
}];
[self.numLabel makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.leakContent.bottom).offset(10);
make.right.equalTo(self.view.right).offset(-20);
make.width.mas_equalTo(@100);
make.height.mas_equalTo(@12);
}];
[self.mailIcon makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.numLabel.bottom).offset(15);
make.left.equalTo(self.view.left).offset(20);
make.width.mas_equalTo(@20);
make.height.mas_equalTo(@15);
}];
[self.mailTextField makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.mailIcon.centerY);
make.left.equalTo(self.mailIcon.right).offset(10);
make.right.equalTo(self.view.right).offset(-20);
make.height.mas_equalTo(@20);
}];
[self.line makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mailIcon.bottom).offset(7);
make.left.equalTo(self.view.left).offset(20);
make.right.equalTo(self.view.right).offset(-20);
make.height.mas_equalTo(@1);
}];
[self.containerView2 makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.line.bottom).offset(35);
make.left.equalTo(self.view.left).offset(20);
make.right.equalTo(self.view.right).offset(-20);
make.height.equalTo(self.containerView2.width).multipliedBy(0.1828);
}];
[self.uploadBtn makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.containerView2.top);
make.left.equalTo(self.containerView2.left);
make.bottom.equalTo(self.containerView2.bottom);
make.width.equalTo(self.uploadBtn.height);
}];
[self.collectionController.view makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.uploadBtn.top);
make.left.equalTo(self.uploadBtn.right);
make.right.equalTo(self.containerView2.right);
make.height.equalTo(self.uploadBtn.height);
}];
[self.uploadLabel makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.uploadBtn.centerY);
make.left.equalTo(self.uploadBtn.right).offset(15);
make.right.equalTo(self.view.right).offset(-20);
make.height.mas_equalTo(@16);
}];
[self.confirmBtn makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.uploadBtn.bottom).offset(35);
make.left.equalTo(self.view.left).offset(20);
make.right.equalTo(self.view.right).offset(-20);
make.height.equalTo(self.phoneBtn.height);
}];
}
//- (void)viewWillLayoutSubviews {
// self.backView.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(self.confirmBtn.frame)+20);
// [super viewWillLayoutSubviews];
//}
- (void)viewDidLayoutSubviews {
self.backView.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(self.confirmBtn.frame)+20);
[super viewDidLayoutSubviews];
}
#pragma mark - get method
- (UIScrollView*)backView {
if (!_backView) {
_backView = [UIScrollView new];
_backView.backgroundColor = [UIColor clearColor];
_backView.showsVerticalScrollIndicator = NO;
_backView.showsHorizontalScrollIndicator = NO;
_backView.scrollEnabled = YES;
}
return _backView;
}
- (TPCustomButton*)phoneBtn {
if (!_phoneBtn) {
_phoneBtn = TPCustomButton.new;
[_phoneBtn addTarget:self action:@selector(callEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _phoneBtn;
}
- (UIImageView*)phoneIcon {
if (!_phoneIcon) {
_phoneIcon = [[UIImageView alloc] init];
_phoneIcon.image = Image(@"setting/leak_phone.png");
}
return _phoneIcon;
}
- (UILabel*)phoneLabel {
if (!_phoneLabel) {
_phoneLabel = [[UILabel alloc] init];
_phoneLabel.text = @"报料热线:4009-20-4009";
_phoneLabel.textColor = [UIColor whiteColor];
_phoneLabel.font = appFont(TEXT_TWO_LEVELSIZE, NO);
_phoneLabel.textAlignment = NSTextAlignmentRight;
}
return _phoneLabel;
}
- (UIView*)containerView1 {
if (!_containerView1) {
_containerView1 = [[UIView alloc] init];
_containerView1.backgroundColor = [UIColor clearColor];
_containerView1.userInteractionEnabled = NO;
}
return _containerView1;
}
- (UITextView*)leakContent {
if (!_leakContent) {
_leakContent = [[UITextView alloc] init];
_leakContent.layer.cornerRadius = 5;
_leakContent.layer.borderWidth =1;
_leakContent.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
_leakContent.textColor = [UIColor colorWithHexString:TextBlack];
_leakContent.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor;
if ([[TPUserDefault instance].isNightMode intValue] > 0)
_leakContent.keyboardAppearance = UIKeyboardAppearanceDark;
else
_leakContent.keyboardAppearance = UIKeyboardAppearanceDefault;
_leakContent.keyboardType = UIKeyboardTypeDefault;
_leakContent.delegate = self;
_leakContent.backgroundColor = [UIColor clearColor];
}
return _leakContent;
}
- (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*)numLabel {
if (!_numLabel) {
_numLabel = [[UILabel alloc] init];
_numLabel.text = @"剩余400字";
_numLabel.textColor = [UIColor colorWithHexString:TextLightGray];
_numLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO);
_numLabel.textAlignment = NSTextAlignmentRight;
_numLabel.backgroundColor = [UIColor clearColor];
}
return _numLabel;
}
- (UIImageView*)mailIcon {
if (!_mailIcon) {
_mailIcon = [[UIImageView alloc]init];
_mailIcon.image = Image(@"setting/leak_mail.png");
}
return _mailIcon;
}
- (UITextField*)mailTextField {
if (!_mailTextField) {
_mailTextField = [[UITextField alloc]init];
_mailTextField.backgroundColor = [UIColor clearColor];
_mailTextField.textColor = [UIColor colorWithHexString:TextBlack];
_mailTextField.textAlignment = NSTextAlignmentLeft;
_mailTextField.keyboardAppearance = UIKeyboardAppearanceDefault;
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
_mailTextField.keyboardAppearance = UIKeyboardAppearanceDark;
}
_mailTextField.keyboardType = UIKeyboardTypeEmailAddress;
_mailTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
_mailTextField.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:@"请输入邮箱/手机号码" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:TextLightGray]}];
_mailTextField.attributedPlaceholder = atrString;
_mailTextField.delegate = self;
[_mailTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
return _mailTextField;
}
- (UIView*)line {
if (!_line) {
_line = [[UIView alloc] init];
_line.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
return _line;
}
- (UIView*)containerView2 {
if (!_containerView2) {
_containerView2 = [[UIView alloc] init];
_containerView2.backgroundColor = [UIColor clearColor];
}
return _containerView2;
}
- (UIButton*)uploadBtn {
if (!_uploadBtn) {
_uploadBtn = [[UIButton alloc] init];
[_uploadBtn setImage:Image(@"setting/leak_addIcon.png") forState:UIControlStateNormal];
[_uploadBtn addTarget:self action:@selector(uploadEvent:) forControlEvents:UIControlEventTouchUpInside];
[_uploadBtn setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:LINECOLOR])
forState:UIControlStateHighlighted];
[_uploadBtn setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:BUTTONDISABLEBACK])
forState:UIControlStateDisabled];
}
return _uploadBtn;
}
- (UILabel*)uploadLabel {
if (!_uploadLabel) {
_uploadLabel = [[UILabel alloc] init];
_uploadLabel.text = @"点击上传图片";
_uploadLabel.textColor = [UIColor colorWithHexString:TextLightGray];
_uploadLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
_uploadLabel.backgroundColor = [UIColor clearColor];
}
return _uploadLabel;
}
- (leakCollectionController*)collectionController {
if (!_collectionController) {
_collectionController = leakCollectionController.new;
_collectionController.delegate = self;
}
return _collectionController;
}
- (UIButton*)confirmBtn {
if (!_confirmBtn) {
_confirmBtn = [[TPCustomButton alloc]init];
_confirmBtn.title = @"报料";
[_confirmBtn addTarget:self action:@selector(confirmEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _confirmBtn;
}
#pragma mark - event
- (void)callEvent:(id)sender {
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
if ([self checkSIM]) {
if (!_phoneMenu) {
_phoneMenu = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles: @"4009-20-4009",nil];
}
[_phoneMenu showInView:self.view];
}
else
ShowTextMessage(@"未安装SIM卡"); //bug5430: 报料:当未安装SIM卡时,点击报料热线,弹出两次提示
}
- (void)uploadEvent:(UIButton*)btn {
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
if ([self.collectionController getNumOfImage] < 4) {
if (!_imageMenu) {
_imageMenu = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles: @"拍照", @"从相册选择",nil];
}
[_imageMenu showInView:self.view];
}
else
ShowTextMessage(@"最多上传4张图片");
}
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//呼出的菜单按钮点击后的响应
if (actionSheet == _imageMenu) {
if (buttonIndex == _imageMenu.cancelButtonIndex) {
NSLog(@"取消");
return;
}
switch (buttonIndex) {
case 0: //打开照相机拍照
[self takePhoto];
break;
case 1: //打开本地相册
[self localPhoto];
break;
default:
break;
}
}
else if (actionSheet == _phoneMenu) {
if (buttonIndex == _phoneMenu.cancelButtonIndex) {
NSLog(@"取消");
return;
}
switch (buttonIndex) {
case 0:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://4009-20-4009"]];
break;
default:
break;
}
}
}
//开始拍照
- (void)takePhoto {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:^{}];
}
}
//打开本地相册
- (void)localPhoto {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:^{}];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
if ([type isEqualToString:@"public.image"]) {
UIImage *originalImage, *editedImage, *imageToUpload;
editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToUpload = editedImage;
} else {
imageToUpload = originalImage;
}
//关闭相册界面
[picker dismissViewControllerAnimated:YES completion:^{}];
[_collectionController addImage:imageToUpload];
if ([self.collectionController getNumOfImage]) {
self.uploadLabel.hidden = YES;
}
}
}
- (void)confirmEvent:(TPCustomButton*)btn {
if (isBlankString(_leakContent.text) || isBlankString(_mailTextField.text)) {
ShowTextMessage(@"报料内容和邮箱/手机号码不得为空");
}
else{
NSString *noEmojiStr = disable_emoji(_leakContent.text);
BOOL isMail = [self isValidateEmail:_mailTextField.text];
BOOL isTel = [self isValidateTelNumber:_mailTextField.text];
if (!isMail && !isTel) {
ShowTextMessage(@"邮箱/手机号码格式有误");
return;
}
_imageList = [_collectionController getImageList];
_imageIDList = [[NSMutableArray alloc] init];
imageUploadFlag = [self.collectionController getNumOfImage];
[self startHud];
__weak typeof(self) Self = self;
if (imageUploadFlag > 0) {
for (UIImage* image in _imageList) {
[self uploadImage:image completion:^(BOOL success) {
imageUploadFlag--;
if (imageUploadFlag == 0) {
if (Self.imageList.count > Self.imageIDList.count) {
[Self stopHud];
ShowTextMessage(@"图片上传失败");
}
else if (Self.imageList.count == Self.imageIDList.count) {
__block NSString* imageIDs = @"";
[Self.imageIDList enumerateObjectsUsingBlock:^(NSString* obj, NSUInteger idx, BOOL *stop) {
if (idx != Self.imageIDList.count-1) {
imageIDs = [imageIDs stringByAppendingString:[NSString stringWithFormat:@"%@,",obj]];
}else {
imageIDs = [imageIDs stringByAppendingString:obj];
}
}];
NSLog(@"%@", imageIDs);
NSDictionary *dic;
if (isMail)
dic = @{@"suggest":noEmojiStr, @"email":_mailTextField.text, @"imageIds":imageIDs};
else
dic = @{@"suggest":noEmojiStr, @"mobile":_mailTextField.text, @"imageIds":imageIDs};
[Remote doJsonActionWithBlock:1 requestUrl:userSuggestURL parameter:dic withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) {
[Self stopHud];
if (success) {
ShowTextMessage(@"你已成功报料");
[Self.navigationController popViewControllerAnimated:YES];
}else{
ShowTextMessage(message);
}
}];
}
}
}];
}
}
else {
NSDictionary *dic;
if (isMail)
dic = @{@"suggest":noEmojiStr, @"email":_mailTextField.text};
else
dic = @{@"suggest":noEmojiStr, @"mobile":_mailTextField.text};
[Remote doJsonActionWithBlock:1 requestUrl:userSuggestURL parameter:dic withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) {
[Self stopHud];
if (success) {
ShowTextMessage(@"你已成功报料");
[Self.navigationController popViewControllerAnimated:YES];
}else{
ShowTextMessage(message);
}
}];
}
}
}
- (void)uploadImage:(UIImage*)image completion:(void(^)(BOOL success))completion {
NSData *imgData = UIImagePNGRepresentation(image);
NSString *type = @"png";
if (imgData == nil) {
imgData = UIImageJPEGRepresentation(image, 1);
type = @"jpg";
}
CGFloat lengh = [imgData length];
NSDictionary *dic = @{@"C_TYPE":type, @"C_LEN":[NSString stringWithFormat:@"%.f",lengh], @"O_TYPE":@"2"};
__weak typeof(self) Self = self;
[Remote upLoadImageAsyncWithBlock:getUploadURL actionTag:0 parameter:dic image:nil completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
NSString *status = responseData[@"STATUS"];
if ([status intValue] != 1 && [status intValue] != 13) {
ShowTextMessage(responseData[@"DESC"]);
if (completion)
completion(NO);
return;
}
NSString *url = responseData[@"URL"];
NSString *fileName = responseData[@"FILE_NAME"];
SaveFile(CachePath(fileName), imgData);
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:responseData];
[dic removeObjectForKey:@"URL"];
[dic setValue:@"0" forKey:@"P_START"];
[dic setValue:dic[@"C_LEN"] forKey:@"P_END"];
[dic removeObjectForKey:@"PARAM"];
[dic removeObjectForKey:@"STATUS"];
[dic removeObjectForKey:@"RESULT"];
[Remote upLoadImageAsyncWithBlock:url actionTag:1 parameter:dic image:CachePath(fileName) completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
TPLOG (@"%@",responseData);
NSString *status = responseData[@"STATUS"];
if ([status intValue] == 13) {
NSString *imageID = responseData[@"IMAGEID"];
[Self.imageIDList addObject:imageID];
if (completion)
completion(YES);
}else {
ShowTextMessage(message);
if (completion)
completion(NO);
}
}else {
ShowTextMessage(message);
if (completion)
completion(NO);
}
}];
}
else {
ShowTextMessage(message);
if (completion)
completion(NO);
}
}];
}
- (void)startHud {
[self.view addSubview:self.waitBackView];
[self.view bringSubviewToFront:self.waitBackView];
self.waitCursor.animating = YES;
}
- (void)stopHud {
[self.waitBackView removeFromSuperview];
self.waitBackView = nil;
self.waitCursor.animating = NO;
}
- (BOOL)isValidateRegularExpression:(NSString *)strDestination byExpression:(NSString *)strExpression {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strExpression];
return [predicate evaluateWithObject:strDestination];
}
//验证email
- (BOOL)isValidateEmail:(NSString *)email {
NSString *strRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{1,5}";
BOOL rt = [self isValidateRegularExpression:email byExpression:strRegex];
return rt;
}
//验证电话号码
- (BOOL)isValidateTelNumber:(NSString *)number {
NSString *strRegex = @"[0-9]{1,20}";
BOOL rt = [self isValidateRegularExpression:number byExpression:strRegex];
return rt;
}
//验证是否安装sim卡
- (BOOL)checkSIM {
BOOL isSimCardAvailable = YES;
CTTelephonyNetworkInfo* info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier* carrier = info.subscriberCellularProvider;
if (carrier.mobileNetworkCode == nil || [carrier.mobileNetworkCode isEqualToString:@""]) {
isSimCardAvailable = NO;
}
return isSimCardAvailable;
}
#pragma mark - delegate
//当用户按下return键或者按回车键,keyboard消失
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
CGFloat distance = self.backView.frame.size.height - CGRectGetMaxY(self.numLabel.frame);
if (keyboardHeight > distance) {
[self.backView setContentOffset:CGPointMake(0, keyboardHeight-distance) animated:YES];
}
else
[self.backView setContentOffset:CGPointMake(0, 0) animated:YES];
}
- (void)keyboardDidShow:(NSNotification *)notification {
if ([self.mailTextField isFirstResponder]) {
NSDictionary *userInfo = [notification userInfo];
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
keyboardHeight = keyboardSize.height;
CGFloat distance = self.backView.frame.size.height - CGRectGetMaxY(self.line.frame);
if (keyboardHeight > distance) {
[self.backView setContentOffset:CGPointMake(0, keyboardHeight-distance) animated:YES];
}
else
[self.backView setContentOffset:CGPointMake(0, 0) animated:YES];
}
if ([self.leakContent isFirstResponder]) {
NSDictionary *userInfo = [notification userInfo];
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
keyboardHeight = keyboardSize.height;
CGFloat distance = self.backView.frame.size.height - CGRectGetMaxY(self.numLabel.frame);
if (keyboardHeight > distance) {
[self.backView setContentOffset:CGPointMake(0, keyboardHeight-distance) animated:YES];
}
else
[self.backView setContentOffset:CGPointMake(0, 0) animated:YES];
}
}
- (void)keyboardDidHide:(NSNotification *)notification {
[self.backView setContentOffset:CGPointMake(0, 0) animated:YES];
}
- (void)imageAllDeleted {
_uploadLabel.hidden = NO;
}
- (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];
NSString *text = [NSString stringWithFormat:@"剩余%lu字", (unsigned long)400-textView.text.length];
_numLabel.text = text;
}
- (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;
}
- (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)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;
}
- (void)tapToDismiss:(id)sender {
[_leakContent resignFirstResponder];
[_mailTextField resignFirstResponder];
}
- (void)startWaitCursor:(int)actionTag {
}
- (void)stopWaitCursor:(int)actionTag {
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
|