2015-06-01 14 views

Trả lời

2

UITextField không có chức năng xác thực ngoài hộp. Bạn có thể tìm thấy một số API nguồn mở để giúp bạn thực hiện việc này. Một tùy chọn có thể là xem xét api SSValidationTextField.

Mã sẽ

var phoneValidationTextField = SSValidationTextField(frame: CGRectMake(200, 200, 150, 50)) 
phoneValidationTextField.validityFunction = self.isValidPhone 
phoneValidationTextField.delaytime = 0.5 
phoneValidationTextField.errorText = "Incorrect Format" 
phoneValidationTextField.successText = "Valid Format" 
phoneValidationTextField.borderStyle = UITextBorderStyle.RoundedRect 
self.addSubview(phoneValidationTextField) 
7

Bạn có thể xác nhận các văn bản bằng cách thiết lập UITextField đại biểu để điều khiển xem của bạn sau đó làm một cái gì đó như thế này:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 

    // Your method to validate the input 
    // self.validateInputText() 

    return true 
} 

Và thậm chí bạn có thể thay đổi màu sắc biên giới của nó nếu bạn muốn:

textField.layer.borderColor = UIColor.redColor().CGColor 

Tôi hy vọng điều này sẽ giúp bạn.

0

Không, không có sẵn phương thức tích hợp sẵn để thực hiện tương tự. Để làm điều đó, bạn cần tùy chỉnh UITextField.

Có một số thư viện nguồn mở có sẵn để thực hiện việc đó. Bạn có thể tìm thấy một địa chỉ tại đây: US2FormValidator

3

Không, bạn cần phải

  1. lớp con các UITextField

  2. Tạo một hàm setError, chúng ta hãy gọi nó func setError()

  3. Trong chức năng đó bạn có thể tạo UIImageView có chứa UIImage (hình ảnh lỗi). Đặt nó vào rightView của UITextField bằng cách sử dụng UITextField.rightView

  4. Đừng quên để thiết lập UITextField.rightViewMode để luôn hiển thị

EDIT:

Ngoài ra, nếu bạn không thích subclassing. Bạn có thể trực tiếp đặt rightVIew của số UITextField thành một số UIImageView giữ hình ảnh lỗi

4

Chỉ cần chia sẻ điều tôi thường sử dụng. Điều này được thiết kế để vừa với TextFields "chỉ có đường viền dưới cùng".- Bởi vì tôi thích chúng;) - nhưng có thể dễ dàng được tùy chỉnh để phù hợp với bất cứ điều gì phong cách

Bottom border only example

mở rộng thành lập các trường văn bản để chỉ hiển thị một điểm mấu chốt duy nhất:

extension UITextField { 
    func setBottomBorderOnlyWith(color: CGColor) { 
     self.borderStyle = .none    
     self.layer.masksToBounds = false 
     self.layer.shadowColor = color 
     self.layer.shadowOffset = CGSize(width: 0.0, height: 1.0) 
     self.layer.shadowOpacity = 1.0 
     self.layer.shadowRadius = 0.0 
    } 
} 

Sau đó, một phần mở rộng khác để làm cho đèn flash màu đỏ và lắc cho thấy có lỗi xác thực:

extension UITextField { 
    func isError(baseColor: CGColor, numberOfShakes shakes: Float, revert: Bool) { 
     let animation: CABasicAnimation = CABasicAnimation(keyPath: "shadowColor") 
     animation.fromValue = baseColor 
     animation.toValue = UIColor.red.cgColor 
     animation.duration = 0.4 
     if revert { animation.autoreverses = true } else { animation.autoreverses = false } 
     self.layer.add(animation, forKey: "") 

     let shake: CABasicAnimation = CABasicAnimation(keyPath: "position") 
     shake.duration = 0.07 
     shake.repeatCount = shakes 
     if revert { shake.autoreverses = true } else { shake.autoreverses = false } 
     shake.fromValue = NSValue(cgPoint: CGPoint(x: self.center.x - 10, y: self.center.y)) 
     shake.toValue = NSValue(cgPoint: CGPoint(x: self.center.x + 10, y: self.center.y)) 
     self.layer.add(shake, forKey: "position") 
    } 
} 

Làm thế nào để sử dụng:

Thiết lập các UITextField hiển thị đường viền đáy chỉ trong viewDidLoad:

override func viewDidLoad() { 
    myTextField.setBottomBorderOnlyWith(color: UIColor.gray.cgColor) 
} 

Sau đó, khi một số nút được nhấp và bạn không xác thực trên sân:

@IBAction func someButtonIsClicked(_ sender: Any) { 
    if let someValue = myTextField, !name.isEmpty { 
     // Good To Go! 
    } else { 
     myTextField.isError(baseColor: UIColor.gray.cgColor, numberOfShakes: 3, revert: true) 
    } 
} 
+0

Câu trả lời hoàn hảo;) –

Các vấn đề liên quan