UITextField的简易封装
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(, , [UIScreen mainScreen].bounds.size.width, height);
self.textField.inputAccessoryView = [[UIView alloc] initWithFrame:rect];
block ? block(self.textField.inputAccessoryView, self) : ;
} - (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 <= ? @"" : 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的简易封装的更多相关文章
- 面localStorage用作数据缓存的简易封装
面localStorage用作数据缓存的简易封装 最近做了一些前端控件的封装,需要用到数据本地存储,开始采用cookie,发现很容易就超过了cookie的容量限制,于是改用localStorage,但 ...
- AVAudioPlayer简易封装
AVAudioPlayer简易封装 [说明] AVAudioPlayer简易封装,仅仅支持播放,暂停,停止,暂停时候带有渐隐效果,自己用,没有参考价值. [源码] https://github.com ...
- 简易封装一个带有占位文字的TextView
在实际iOS应用开发中我们经常会用到类似于下图所示的界面,即带有占位文字的文本框:
- 对xlslib库与libxls库的简易封装
一.简介 xlslib库是用来创建excel文件.libxls是用来读取excel文件的,在使用C++或者QT语言来设计对excel文件的读取.都需要事先下载这两个库编译成功后再进行程序设计的.之所以 ...
- node.js + mssql 简易封装操作
时间吧,总是这么凑巧,在我学习[node.js]还没几天,我的 Microsoft SQL Server Management Studio 18 就歇菜了,至于怎么歇菜的吧....它可能的意思就是想 ...
- 基于Vue简易封装的快速构建Echarts组件 -- fx67llQuickEcharts
fx67llQuickEcharts A tool to help you use Echarts quickly! npm 组件说明 这本来是一个测试如何发布Vue组件至npm库的测试项目 做完之后 ...
- jQuery版AJAX简易封装
开发过程中,AJAX的应用应该说非常频繁,当然,jQuery的AJAX函数已经非常好用,但是小编还是稍微整理下,方便不同需求下,可以简化输入参数,下面是实例代码: $(function(){ /** ...
- 页面localStorage用作数据缓存的简易封装
最近做了一些前端控件的封装,需要用到数据本地存储,开始采用cookie,发现很容易就超过了cookie的容量限制,于是改用localStorage,但localStorage过于简单,没有任何管理和限 ...
- Nhibernate基础使用教程以及简易封装
1.Nhibernate简介 NHibernate是一个面向.NET环境的对象/关系数据库映射工具.对象/关系数据库映射(object/relational mapping,ORM)这个术语表示一种技 ...
随机推荐
- java 代理 agency
java并没有对其提供直接的支持,这是继承和组合的中庸之道,因为我们将一个成员对象置于所要构造的类中(组合),但与此同时我们在新类中暴露了该成员的所有方法(就像继承),使用代理时可以拥有更多的控制力, ...
- PHP跨域访问
1.允许所有域名访问 header('Access-Control-Allow-Origin: *'); 2.允许单个域名访问 header('Access-Control-Allow-Origin: ...
- SqlServer Case when then用法总结
SELECT d.DicName , --DevelopMode ISNULL(NULL,NULL) , --Orgid b.FullName , --Areid c.DicName , --Inve ...
- 013 jquery中关于表格行的增删问题
1.程序 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- Spring拦截器和过滤器
什么是拦截器 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对象,允许开发者 ...
- 001.SMB简介
一 简介 samba基于NetBIOS协议开发,能和windows通信,但只能在局域网通信. 二 Samba主要应用 文件共享 打印服务器 Samba登录时身份验证 进行windows主机名解析 三 ...
- maven自定义脚手架(快速生成项目)
Maven之自定义archetype生成项目骨架 利用脚手架生成 新项目 命令行方式 mvn archetype:generate \ -DarchetypeGroupId=com.xxx \ -Da ...
- SpringBoot详细研究-05微服务与拾遗
这将是SpringBoot学习系列的最后一篇文章,主要涉及Spring Cloud和JHispter两个知识点,都具有一定综合性,需要多加实践加强理解. Spring Cloud 微服务Microse ...
- BEM
BEM代表块(Block),元素(Element),修饰符(Modifier).BEM是由Yandex团队提出的一种CSS Class 命名方法,旨在更好的创建CSS/Sass模块.通过增加前缀的办法 ...
- [POI2013]Taksówki
[POI2013]Taksówki 题目大意: ABC三地在同一条直线上,AC相距\(m(m\le10^{18})\)米,AB相距\(d\),B在AC之间.总共有\(n(n\le5\times10^5 ...