码迷,mamicode.com
首页 > 其他好文 > 详细

UITextField的简易封装

时间:2016-07-24 22:31:58      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:

UITextField的简易封装

技术分享

 

效果

技术分享

 

源码

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

//
//  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 should begin in the specified text field.
 *
 *  @param textFieldView UITextFieldView object.
 *
 *  @return YES if editing should begin or NO if it should 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 should stop in the specified text field.
 *
 *  @param textFieldView UITextFieldView object.
 *
 *  @return YES if editing should stop or NO if it should 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 should 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 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 should be replaced; otherwise, NO to keep the old text.
 */
typedef BOOL (^textFieldshouldChangeCharactersInRange_t)(UITextFieldView *textFieldView, NSRange range, NSString *replacementString, NSString *currentText);

/**
 *  Asks the delegate if the text field’s current contents should be removed.
 *
 *  @param textFieldView UITextFieldView object.
 *
 *  @return YES if the text field’s contents should be cleared; otherwise, NO.
 */
typedef BOOL (^textFieldShouldClear_t)(UITextFieldView *textFieldView);

/**
 *  Asks the delegate if the text field should process the pressing of the return button.
 *
 *  @param textFieldView YES if the text field should 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 many 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. 

/**
 *  Should begin editing block.
 */
@property (nonatomic, copy) textFieldShouldBeginEditing_t shouldBeginEditingBlock;

/**
 *  Did begin editing block.
 */
@property (nonatomic, copy) textFieldDidBeginEditing_t didBeginEditingBlock;

/**
 *  should end editing block.
 */
@property (nonatomic, copy) textFieldShouldEndEditing_t shouldEndEditingBlock;

/**
 *  Did end editing block.
 */
@property (nonatomic, copy) textFieldDidEndEditing_t didEndEditingBlock;

/**
 *  Should change characters in range block.
 */
@property (nonatomic, copy) textFieldshouldChangeCharactersInRange_t shouldChangeCharactersInRangeBlock;

/**
 *  Should clear block.
 */
@property (nonatomic, copy) textFieldShouldClear_t shouldClearBlock;

/**
 *  Should return block.
 */
@property (nonatomic, copy) textFieldShouldReturn_t shouldReturnBlock;

/**
 *  Convenient method to set blocks.
 *
 *  @param changeCharactersInRange Should change characters in range block.
 *  @param didBeginEditingBlock    Did begin editing block.
 *  @param didEndEditingBlock      Did end editing block.
 *  @param shouldReturnBlock       Did end editing block.
 */
- (void)registerShouldChangeCharactersInRange:(textFieldshouldChangeCharactersInRange_t)changeCharactersInRange
                              didBeginEditing:(textFieldDidBeginEditing_t)didBeginEditingBlock
                                didEndEditing:(textFieldDidEndEditing_t)didEndEditingBlock
                                 shouldReturn:(textFieldShouldReturn_t)shouldReturnBlock;

#pragma mark - Become & resign first responder.

/**
 *  Notifies the receiver that it is about to become first responder in its window.
 */
- (void)becomeFirstResponder;

/**
 *  Notifies the receiver 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         secureTextEntryBecomeActive;

@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:(textFieldDidBeginEditing_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 *)text {

    _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.shouldBeginEditingBlock) {
        
        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 self.shouldEndEditingBlock(self);
        
    } else {
    
        return YES;
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {

    if (self.didEndEditingBlock) {
        
        self.didEndEditingBlock(self);
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if (self.textField.secureTextEntry == YES && _secureTextEntryBecomeActive == YES) {
        
        // 密码键盘特殊处理
        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 textFieldValidatorMessageWithErrorMessage: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 *)textFieldValidatorMessageWithErrorMessage:(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

 

UITextField的简易封装

标签:

原文地址:http://www.cnblogs.com/YouXianMing/p/5701571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!