美文网首页
UITextField

UITextField

作者: L柠_檬 | 来源:发表于2016-08-19 13:41 被阅读10次
    目录
      1.1 设置返回键样式
      1.2 设置边框样式
      1.3 一键删除
      1.4 密文
      1.5 添加左右视图
      1.6 监听键盘出现
      1.7 放弃第一响应者
      1.8 数字键盘
      1.9 手机号验证
      1.10 return键点击
    
    1.1 设置返回键样式
    
    textField.returnKeyType=UIReturnKeyDone;
    
    
    1.2 设置边框样式
    
    textField.borderStyle=UITextBorderStyleRoundedRect;
    
    1.3 一键删除
    
    textField.clearButtonMode=UITextFieldViewModeAlways;
    
    
    1.4 密文
    
    textField.secureTextEntry=NO;
    
    1.5 添加左右视图
    
    UIImage *image=[UIImage imageNamed:@"account_candou"];
    
    UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0,0 ,
     50, 50)];
    
    imageview.image=image;
    
    textField.leftView=imageview;
    
    textField.leftViewMode=UITextFieldViewModeAlways;
    
    1.6 监听键盘出现
    
    1.出现
    
    [[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(keyboardWillShow:) 
    name:UIKeyboardWillShowNotification object:nil];
    
    2.消失
    
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(keyboardWillHidden:) 
    name:UIKeyboardWillHideNotification object:nil];
    
    3.实现方法
    
    - (void)keyboardWillShow:(NSNotificationCenter *)noti{
        
        self.tableView.frame = CGRectMake(0, 0, 
        SCREEN_WIDTH, SCREEN_HEIGHT - 280);
        
    }
    
    - (void)keyboardWillHidden:(NSNotificationCenter *)noti{
        
        self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    }
    
    1.7 放弃第一响应者
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField;
    {
    [textField resignFirstResponder];
    
    return YES;
    
    }
    
    1.8 数字键盘
    
    textField.keyboardType = UIKeyboardTypePhonePad;
    
    
    
    1.9 手机号验证
    
    记得写代理
    
    - (BOOL)textField:(UITextField*)textField 
    shouldChangeCharactersInRange:(NSRange)range 
                replacementString:(NSString*)string {
        NSCharacterSet *cs;
        
        cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] 
        invertedSet];
        
        NSString*filtered = [[string componentsSeparatedByCharactersInSet:cs]
                            componentsJoinedByString:@""];
        
        BOOL basicTest = [string isEqualToString:filtered];
        
        if(!basicTest) {
            
            return NO;
            
        }
        
        NSString *temp = [textField.text 
                    stringByReplacingCharactersInRange:range 
                                            withString:string];
        
        if (temp.length > 11) {
            
            return NO;
            
        }
        
        return YES;
        
    }
    
    1.10 return键点击
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
        return YES;
    
    }
    

    相关文章

      网友评论

          本文标题:UITextField

          本文链接:https://www.haomeiwen.com/subject/ubebsttx.html