Simple encapsulation of UITextField and uitextfield Encapsulation

Source: Internet
Author: User

Simple encapsulation of UITextField and uitextfield Encapsulation

Simple UITextField Encapsulation

 

Effect

 

Source code

UITextFieldView in https://github.com/YouXianMing/UI-Component-Collection

/// UITextFieldView. h // UITextField /// Created by YouXianMing on 16/7/22. // Copyright©2016 YouXianMing. all rights reserved. // # import <UIKit/UIKit. h> # import "AbsTextFieldViewValidator. h "@ class UITextFieldView; @ protocol UITextFieldViewDelegate <NSObject> @ optional/*** When change characters in range, you can get the current string. ** @ param textFieldView UITextFieldView's object. * @ param currentString The current string. */-(void) textFieldView :( UITextFieldView *) textFieldView currentString :( NSString *) currentString; @ end/*** Asks the delegate if editing shold begin the specified text field. ** @ param textFieldView UITextFieldView object. ** @ return YES if editing shoshould begin or NO if it shoshould not. */typedef BOOL (^ textFieldShouldBeginEditing_t) (UITextFieldView * textFieldView);/*** Tells the delegate that editing began in the specified text field. ** @ param textFieldView UITextFieldView object. */typedef void (^ textFieldDidBeginEditing_t) (UITextFieldView * textFieldView);/*** Asks the delegate if editing shoshould stop in the specified text field. ** @ param textFieldView UITextFieldView object. ** @ return YES if editing shoshould stop or NO if it shoshould continue. */typedef BOOL (^ textFieldShouldEndEditing_t) (UITextFieldView * textFieldView);/*** Tells the delegate that editing stopped for the specified text field. ** @ param textFieldView UITextFieldView object. */typedef void (^ textFieldDidEndEditing_t) (UITextFieldView * textFieldView);/*** Asks the delegate if the specified text shocould be changed. ** @ param textFieldView UITextFieldView object. * @ param range The range of characters to be replaced. * @ param replacementString The replacement string for the specified range. during typing, this parameter normally contains only the single new character that was typed, but it may contain in more characters if the user is pasting text. when the user deletes one or more characters, the replacement string is empty. * @ param currentText The current string. ** @ return YES if the specified text range shocould be replaced; otherwise, NO to keep the old text. */typedef BOOL (^ textFieldshouldChangeCharactersInRange_t) (UITextFieldView * textFieldView, nsange range, NSString * replacementString, NSString * currentText ); /*** Asks the delegate if the text field's current contents shocould be removed. ** @ param textFieldView UITextFieldView object. ** @ return YES if the text field's contents shoshould be cleared; otherwise, NO. */typedef BOOL (^ textFieldShouldClear_t) (UITextFieldView * textFieldView);/*** Asks the delegate if the text field shocould process the pressing of the return button. ** @ param textFieldView YES if the text field shocould implement its default behavior for the return button; otherwise, NO. ** @ return UITextFieldView object. */typedef BOOL (^ textFieldShouldReturn_t) (UITextFieldView * textFieldView); # pragma mark-UITextFieldView @ interface UITextFieldView: UIView/*** UITextFieldView's delegate. */@ property (nonatomic, weak) id <UITextFieldViewDelegate> delegate;/*** To set the textField's text & currentText's text. ** @ param text The text you set. */-(void) setCurrentTextFieldText :( NSString *) text;/*** The textField, you can use it to set custom properties. */@ property (nonatomic, strong, readonly) UITextField * textField;/*** The current string. */@ property (nonatomic, strong, readonly) NSString * currentText; # pragma mark-TextField validator. /*** TextField validator. */@ property (nonatomic, strong) AbsTextFieldViewValidator * textFieldViewValidator;/*** Checking the textField's string. ** @ return TextField validator message. */-(TextFieldValidatorMessage *) checkingTheTextFieldViewString; # pragma mark-TextField delegate's block. /*** shoshould begin editing block. */@ property (nonatomic, copy) textFieldShouldBeginEditing_t shouldBeginEditingBlock;/*** Did begin editing block. * // @ property (nonatomic, copy) textFieldDidBeginEditing_t didBeginEditingBlock;/*** shocould end editing block. */@ property (nonatomic, copy) textFieldShouldEndEditing_t shouldEndEditingBlock;/*** Did end editing block. */@ property (nonatomic, copy) textFieldDidEndEditing_t didEndEditingBlock;/*** shoshould change characters in range block. */@ property (nonatomic, copy) textFieldshouldChangeCharactersInRange_t shouldChangeCharactersInRangeBlock;/*** shoshould clear block. */@ property (nonatomic, copy) textFieldShouldClear_t shouldClearBlock;/*** shold return block. */@ property (nonatomic, copy) textFieldShouldReturn_t shouldReturnBlock;/*** Convenient method to set blocks. ** @ param changeCharactersInRange shoshould change characters in range block. * @ param didBeginEditingBlock Did begin editing block. * @ param didEndEditingBlock Did end editing block. * @ param shouldReturnBlock Did end editing block. */-(void) warn :( warn) includidbeginediting :( warn) includidendediting :( warn) didEndEditingBlock shouldReturn :( warn) shouldReturnBlock; # pragma mark-Become & resign first responder. /*** Notifies the handler er that it is about to become first responder in its window. */-(void) becomeFirstResponder;/*** Notifies the specified er that it has been asked to relinquish its status as first responder in its window. */-(void) resignFirstResponder; # pragma mark-InputAccessoryView. -(void) createInputAccessoryViewWithViewHeight :( CGFloat) height block :( void (^) (UIView * inputAccessoryView, UITextFieldView * textFieldView) block; # pragma mark-Transform position. /*** Rect from the view. ** @ param view The view you specified. ** @ return The rect. */-(CGRect) rectFromView :( UIView *) view; # pragma mark-Constructor method. //-(instancetype) @ end
/// UITextFieldView. m // UITextField /// Created by YouXianMing on 16/7/22. // Copyright©2016 YouXianMing. all rights reserved. // # import "UITextFieldView. h "@ interface UITextFieldView () <UITextFieldDelegate> @ property (nonatomic, strong) NSString * currentText; @ property (nonatomic, strong) UITextField * textField; @ property (nonatomic) BOOL comment; @ end @ implementation UITextFieldView-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {Self. textField = [[UITextField alloc] initWithFrame: self. bounds]; self. textField. delegate = self; [self addSubview: self. textField];} return self;}-(void) becomeFirstResponder {[self. textField becomeFirstResponder];}-(void) resignFirstResponder {[self. textField resignFirstResponder];}-(void) registerShouldChangeCharactersInRange :( textFieldshouldChangeCharactersInRange_t) block didBeginEditing :( t ExtFieldDidBeginEditing_t) didBeginEditingBlock didEndEditing :( textFieldDidEndEditing_t) didEndEditingBlock shouldReturn :( textFieldShouldReturn_t) shouldReturnBlock {self. shouldChangeCharactersInRangeBlock = block; self. shouldReturnBlock = shouldReturnBlock; self. didBeginEditingBlock = didBeginEditingBlock; self. didEndEditingBlock = didEndEditingBlock;}-(void) setCurrentTextFieldText :( NSString *) tex T {_ currentText = text; _ textField. text = text;}-(void) createInputAccessoryViewWithViewHeight :( CGFloat) height block :( void (^) (UIView * inputAccessoryView, UITextFieldView * textFieldView) block {CGRect rect = CGRectMake (0, 0, [UIScreen mainScreen]. bounds. size. width, height); self. textField. inputAccessoryView = [[UIView alloc] initWithFrame: rect]; block? Block (self. textField. inputAccessoryView, self): 0;}-(TextFieldValidatorMessage *) checkingTheTextFieldViewString {return [self. textFieldViewValidator validatorWithInputSting: self. currentText];}-(CGRect) rectFromView :( UIView *) view {return [self convertRect: self. bounds toView: view] ;}# pragma mark-UITextFieldDelegate-(BOOL) textFieldShouldBeginEditing :( UITextField *) textField {if (self. shouldBe GinEditingBlock) {return self. shouldBeginEditingBlock (self);} else {return YES;}-(void) textFieldDidBeginEditing :( UITextField *) textField {if (self. didBeginEditingBlock) {self. didBeginEditingBlock (self);} if (self. textField. secureTextEntry = YES) {_ secureTextEntryBecomeActive = YES;}-(BOOL) textFieldShouldEndEditing :( UITextField *) textField {if (self. shouldEndEditingBlock) {return se Lf. shouldEndEditingBlock (self);} else {return YES;}-(void) textFieldDidEndEditing :( UITextField *) textField {if (self. didEndEditingBlock) {self. didEndEditingBlock (self) ;}}-(BOOL) textField :( UITextField *) textField shouldChangeCharactersInRange :( nsange) range replacementString :( NSString *) string {if (self. textField. secureTextEntry = YES & _ secureTextEntryBecomeActive = YES) {// special handling of password and keyboard Self. currentText = [NSMutableString stringWithString: string. length <= 0? @ "": String]; self. secureTextEntryBecomeActive = NO;} else {// NSString * currentText = [textField. text stringByReplacingCharactersInRange: range withString: string]; self. currentText = currentText;} if (self. delegate & [self. delegate respondsToSelector: @ selector (textFieldView: currentString :)]) {[self. delegate textFieldView: self currentString: self. currentText];} if (self. shouldChangeCharactersInRangeBlock) {return self. shouldChangeCharactersInRangeBlock (self, range, string, self. currentText);} else {return YES;}-(BOOL) textFieldShouldClear :( UITextField *) textField {if (self. shouldClearBlock) {return self. shouldClearBlock (self);} else {return YES;}-(BOOL) textFieldShouldReturn :( UITextField *) textField {if (self. shouldReturnBlock) {return self. shouldReturnBlock (self);} else {return YES ;}}@ end
/// TextFieldValidatorMessage. h // ZiPeiYi // Created by YouXianMing on 16/1/8. // Copyright©2016 YouXianMing. all rights reserved. // # import <Foundation/Foundation. h> @ interface TextFieldValidatorMessage: NSObject/*** Is valid string or not. */@ property (nonatomic) BOOL isValidString;/*** Error message. */@ property (nonatomic, strong) NSString * errorMessage;/*** Convenient method. ** @ param errorMessage Error message string. * @ param isValidString Is valid string or not. ** @ return TextFieldValidatorMessage. * // + (TextFieldValidatorMessage *) textFieldValidatorMessageWithErrorMessage :( NSString *) errorMessage isValidString :( BOOL) isValidString; @ end/*** Convenient method. ** @ param isValidString Is valid string or not. * @ param errorMessage Error message string. ** @ return TextFieldValidatorMessage. */NS_INLINE TextFieldValidatorMessage * textFieldValidatorMessageIsValid (BOOL isValidString, NSString * errorMessage) {return [TextFieldValidatorMessage identifier: errorMessage isValidString: isValidString];}
/// TextFieldValidatorMessage. m // ZiPeiYi // Created by YouXianMing on 16/1/8. // Copyright©2016 YouXianMing. all rights reserved. // # import "TextFieldValidatorMessage. h "@ implementation TextFieldValidatorMessage + (TextFieldValidatorMessage *) messages :( NSString *) errorMessage isValidString :( BOOL) isValidString {TextFieldValidatorMessage * message = [[self class] new]; message. errorMessage = errorMessage; message. isValidString = isValidString; return message;} @ end
/// AbsTextFieldViewValidator. h // UITextField // Created by YouXianMing on 16/7/23. // Copyright©2016 YouXianMing. all rights reserved. // # import <Foundation/Foundation. h> # import "TextFieldValidatorMessage. h "@ interface AbsTextFieldViewValidator: NSObject-(TextFieldValidatorMessage *) validatorWithInputSting :( NSString *) inputString; @ end
/// AbsTextFieldViewValidator. m // UITextField // Created by YouXianMing on 16/7/23. // Copyright©2016 YouXianMing. all rights reserved. // # import "AbsTextFieldViewValidator. h "@ implementation AbsTextFieldViewValidator-(TextFieldValidatorMessage *) validatorWithInputSting :( NSString *) inputString {return textFieldValidatorMessageIsValid (YES, nil);} @ end

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.