TextField 相关

2016-09-23  本文已影响0人  Mn_Su

第一、点击textfield上移视图(键盘上部按钮或者标签)

** 一.通知中心**

    1.初始化通知中心,设置控制器为监听器
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    2.监听器消失时,移除通知中心
        - (void)dealloc{
               [[NSNotificationCenter defaultCenter] removeObserver:self];

        }
    3.实现监听器的方法
        - (void)KeyboardWillChangeFrame:(NSNotification *)note{ 
                NSLog(@"%@",note.userInfo);

                CGRect keyboard = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
                CGFloat trans = keyboard.origin.y - HEIGHT;
                self.view.transform = CGAffineTransformMakeTranslation(0, trans);
    
        }

    4.拖动或点击textfield上部视图时,键盘消失
            - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
                [self.view endEditing:YES];         
            }
         或者是
            - (void)touchesBegan:(NSSet<UITouch> *)touches withEvent:(UIEvent *)event{
                [self.view endEditing:YES];
            }

** 二.textfield的代理方法**

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    
    CGRect frame = textView.frame;
    
    int offset = frame.origin.y + 80 - (HEIGHT - 216.0);//iPhone键盘高度216,iPad的为352
    
    [UIView animateWithDuration:0.5 animations:^{
        
        //将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
        
        if(offset > 0)
            
            self.view.frame = CGRectMake(0, -offset, WIDTH, HEIGHT);
            [self.navigationController setNavigationBarHidden:NO animated:YES];
        
    }];

    return YES;
}

-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    [UIView animateWithDuration:0.2 animations:^{
        self.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    }];
    
    return YES;
}

**三、工具栏 **

     //创建工具条
     UIToolbar *toolbar=[[UIToolbar alloc]init];
     //设置工具条的颜色
     toolbar.barTintColor=[UIColor brownColor];
     //设置工具条的frame
     toolbar.frame=CGRectMake(0, 0, 320, 44);
     
     //给工具条添加按钮
        UIBarButtonItem *item0=[[UIBarButtonItem alloc]initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:self action:@selector(click) ];
     
         UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:self action:@selector(click)];
     
         UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
         UIBarButtonItem *item3=[[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(click)];
     
      toolbar.items = @[item0, item1, item2, item3];
     //设置文本输入框键盘的辅助视图
     self.textfield.inputAccessoryView=toolbar;

** 四.第三方库 IQKeyboardManager**

** 超级传送门**

第二、监听输入框删除键

    1.新建类文件,集成系统 UItextField
    2.添加删除键代理 A
    3.重写 deleteBackward 方法
    4.在需要用的类文件中,添加代理,实现 删除键代理A

    举例:
    1、MSUTextField.h 文件:

        #import <UIKit>

        @protocol MSUTextFieldDelegate <NSObject>
        
        - (void)deleteBtnClick:(UITextField *)textField;
        
        @end
        
        @interface MSUTextField : UITextField
        
        @property (nonatomic , weak) id<MSUTextFieldDelegate> delegate;
        
        
        @end

    2、MSUTextField.m 文件:重写 deleteBackward 方法

        #import "MSUTextField.h"

        @implementation MSUTextField
        
        - (void)deleteBackward{
            [super deleteBackward];
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(deleteBtnClick:)]) {
                [self.delegate deleteBtnClick:self];
            }
            
        }
        
        @end

    3、要用的类文件 :MSUAgainSetPWDController.m 中

        1)遵循代理协议 <MSUTextFieldDelegate>
        2) 实现代理方法 deleteBtnClick
            - (void)deleteBtnClick:(UITextField *)textField{
                if (textField.text.length > 0 || textField.text.length == 0) {
                    UILabel *label = self.btnArr[textField.text.length];
                    label.text = @"";
                    self.pwd2Str = textField.text;
                }
            }

第三、更改输入框占位符字体大小、颜色等

        self.shopTF = [[UITextField alloc] init];
        _shopTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"商品名称" attributes:@{NSForegroundColorAttributeName: HEXCOLOR(0x757575)}];
        _shopTF.frame = CGRectMake(14, 0, WIDTH-28, 40);
        _shopTF.font = [UIFont systemFontOfSize:14];
        _shopTF.textColor = HEXCOLOR(0x333333);
        //        _shopTF = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 30)];
        //        _shopTF = UITextFieldViewModeAlways;
        [topView addSubview:_shopTF];

第四、手机号码以及密码输入及相关限制(注: 如果想要单独某个控制器调用系统键盘,设置 textField的secureTextEntry = NO;)

- (NSString *)md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"xxxxxxxxxxxxxxxx",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}



- (void)textFieldDidEndEditing:(UITextField *)textField{
    if ([textField.text isEqualToString:_phoneTF.text]) {
        [self checkTel:_phoneTF.text];
    }else if ([textField.text isEqualToString:_passwordTF.text]){
        [self checkPassWord:_passwordTF.text];
    }
}


- (BOOL)checkTel:(NSString *)str{
    if ([str length] == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"手机号码不能为空" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
        
        return NO;
    }
    
    //正则表达式
    NSString *regex = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[0678])\\d{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    BOOL isMatch = [pred evaluateWithObject:str];
    if (!isMatch) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入正确的手机号码" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
       
        return NO;
    }
    return YES;
}

- (BOOL)checkPassWord:(NSString *)str{
    if (_passwordTF.text.length < 6> 16) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"密码应为6-16位,区分大小写" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
        
        return NO;
    }
    
    //***需要过滤的特殊字符:~¥#&*<>《》()[]{}【】^@/£¤¥|§¨「」『』¢¬ ̄~@#¥&*()——+|《》$_€。
    NSRange urgentRange = [str rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"~¥#&*<>《》()[]{}【】^@/£¤¥|§¨「」『』¢¬ ̄~@#¥&*()——+|《》$_€"]];
    if (urgentRange.location == NSNotFound) {
        
        return NO;
    }
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"温馨提示", nil) message:NSLocalizedString(@"您的密码中应不包含特殊字符", nil) preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
    return YES;

- (void) touchesBegan:(NSSet<UITouch> *)touches withEvent:(UIEvent *)event{
        [self.view endEditing:YES];
}
上一篇下一篇

猜你喜欢

热点阅读