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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容