UITextField小记

  1. 对 UITextField进行内容判断
  NSScanner* scan = [NSScanner scannerWithString:self.countTextField.text];
        int val;
        NSString *string = [NSString stringWithFormat:@"%d",[scan scanInt:&val] && [scan isAtEnd]];
        NSLog(@"%d",[scan scanInt:&val] && [scan isAtEnd]);
        if ([string isEqualToString:@"0"]) {
            // 0 代表不全为数字
        }else {
            // 1 代表输入的全为数字
        }
  1. 修改UITextField中placeholder的字体颜色
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] =Color(201,197,189);
    NSAttributedString *attribute = [[NSAttributedString alloc]initWithString:self.emailText.placeholder attributes:dict];
    [self.emailText setAttributedPlaceholder:attribute];
  1. textField 不顶格输入
    tel.leftView = [[UIView alloc]initWithFrame:CGRectMake(0,0,8,0)];
    tel.leftViewMode =UITextFieldViewModeAlways;
  1. textField 密文输入
    password.secureTextEntry =YES;
  1. 限制texfield输入位数(例如以下,最多输入4位)
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.authorizationPassword) {
        if (string.length == 0) return YES;
        
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        if (existedLength - selectedLength + replaceLength > 4) {
            return NO;
        }
    }
    return YES;
}

你可能感兴趣的:(UITextField小记)