UITextField Validation visual feedback

▼魔方 西西 提交于 2019-12-04 03:24:58

It's pretty easy to add an 'warning' image to the left-hand side of a UITextField to indicate that the field needs a value.

Try this:

UITextField* field = .... your text field ...

if ( fails_validation ) {
  field.leftViewMode = UITextFieldViewModeAlways;
  UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
  imageView.image = [UIImage imageNamed:@"warning.png"];
  imageView.contentMode = UIViewContentModeScaleAspectFit;
  field.leftView = imageView;
} else {
  field.leftViewMode = UITextFieldViewModeNever;
  field.leftView = nil;
}

The best two options I've found so far are TextFieldValidator and US2FormValidator. With the caveat that I have only used the former, here's my take on each.

TextFieldValidator

  • ↑ Simple (just one class!)
  • ↑ Offers default UI with additional customization possible
  • ↑ Handles multiple validations on a single field
  • ↓ Doesn't handle text views
  • ↓ Slightly awkward name…ValidatedTextField, for example, would be more accurate :)

US2FormValidator

  • ↑ Handles text fields and text views
  • ↑ Importable as a framework, including CocoaPods support
  • ↑ Handles multiple validations on a single field
  • ↓ No default UI

If you just need something implemented quickly, TextFieldValidator may be all you need. If you must have validated UITextViews, US2FormValidator is the way to go.

Have a look at Managing Overlay Views section in UITextField docs

Put a check mark to the right of the UITextField when correct, Else put an X to the right of it. to make it look smooth: fade it using an animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!