VLRTextField 0.0.5

VLRTextField 0.0.5

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最近发布2015年11月

Marian PaulSébastien HOUZÉLucas Ortis 维护。



 
依赖项
PPHelpMe~> 1.0.2
VLRJVFloatLabeledTextField~> 1.0.3
 


  • Rezzza

VeryLastRoom iOS textField

alt tag alt tag

目的

这个类旨在为两个优秀的库在UITextField上提供扩展功能

VLRTextfield增加了一个新的层面: 验证(例如,在表单中非常好用)。然后可以指定几个检查行为,如

  • 使用正则表达式检查内容 (查看regex) AND/OR
  • 填充可选或不填(查看 fillRequired) AND/OR
  • 输入字符的最小数量(例如:密码为8个字符)(查看 minimumNumberOfCharacters) AND/OR
  • 自定义验证块(查看 validateBlock).

所有这些测试都可以在需要时或在编辑时运行。

示例

经典文本框

VLRTextField *name          = [VLRTextField myAppCustomizedTextFieldWithPlaceholder:@"Enter your first name"];
name.autocapitalizationType = UITextAutocapitalizationTypeWords;
name.formKeyPath            = @"name";
name.messageRequired        = @"Your first name is required";

正则表达式文本框

static NSString *REGULAR_EXPRESSION_EMAIL = @"[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}";

VLRTextField *email          = [VLRTextField myAppCustomizedTextFieldWithPlaceholder:@"Enter an email to reach you"];
email.keyboardType           = UIKeyboardTypeEmailAddress;
email.regex                  = REGULAR_EXPRESSION_EMAIL;
email.messageInvalid         = @"Your email address is invalid";
email.messageRequired        = @"Please enter an email adress";
email.formKeyPath            = @"email_address";
email.autocompleteDataSource = [VLRAutoCompleteManager sharedManager];
email.autocompleteType       = VLRAutocompleteTypeEmail;

注意:VLRAutoCompleteManager。查看HTAutocompleteTextField

最小字符数文本框

VLRTextField *password             = [VLRTextField myAppCustomizedTextFieldWithPlaceholder:@"Enter a password"];
password.minimumNumberOfCharacters = 8;
password.messageRequired           = @"The password should be 8 characters long";
password.formKeyPath               = @"password_1";
password.secureTextEntry           = YES;

自定义验证块文本框

我们使用该块来检查两个密码是否相同

VLRTextField *passwordConfirmation             = [VLRTextField formTextFieldWithFrame:CGRectMake(kXOffset, kYOffset + CGRectGetMaxY(password.frame), width, kTextFieldHeight) placeholder:@"Re enter your password"];
passwordConfirmation.messageInvalid            = @"The two passwords should match";
passwordConfirmation.messageRequired           = @"Please re enter your password";
passwordConfirmation.minimumNumberOfCharacters = password.minimumNumberOfCharacters;
passwordConfirmation.formKeyPath               = @"password_2";
passwordConfirmation.secureTextEntry           = YES;
passwordConfirmation.validateBlock             = ^BOOL(VLRTextField *textField) {
    return [[password text] isEqualToString:textField.text];
};

非必填文本框

VLRTextField *company          = [VLRTextField formTextFieldWithFrame:CGRectMake(kXOffset, kYOffset + CGRectGetMaxY(passwordConfirmation.frame), width, kTextFieldHeight) placeholder:@"Enter the company you work for"];
company.fillRequired           = NO;
company.formKeyPath            = @"company_name";
company.autocompleteDataSource = [VLRAutoCompleteManager sharedManager];
company.autocompleteType       = VLRAutoCompleteCompany;
company.autocapitalizationType = UITextAutocapitalizationTypeSentences;
company.returnKeyType          = UIReturnKeyDone;

注意:VLRAutoCompleteManager。查看HTAutocompleteTextField

还有一件事

除了验证用户输入的内容外,还可以使用VLRFormService

  • 自动跳转到下一个界面,如果 `returnKeyType` 等于 `UIReturnKeyNext
  • 检查表单并显示错误信息(或不在显示)
  • 将其注册的文本框的JSON提取为NSDictionary

如果有必要,可以子类化 VLRFormService 以添加符合您需求的新行为。

如何使用 VLRFormService

self.registerTextFieldManager          = [VLRFormService new];

// The order does matter (for next behavior)
[self.registerTextFieldManager addTextField:name];
[self.registerTextFieldManager addTextField:email];
[self.registerTextFieldManager addTextField:password];
[self.registerTextFieldManager addTextField:passwordConfirmation];
[self.registerTextFieldManager addTextField:company];

检查表单

检查并显示错误

- (void) send {
    BOOL formValid = [self.registerTextFieldManager checkForm]; // Will check and display errors
    if (formValid) {
        [self.registerTextFieldManager.activeField resignFirstResponder];
        [self safelySend];
    }
}

检查但不显示错误

BOOL formValid = [self.registerTextFieldManager checkFormAndShowErrors:NO];

提取JSON

这将使用 VLRTextField 上的 formKeyPath 属性

NSDictionary *json = [self.registerTextFieldManager extractFieldsAsJson];