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的简易封装的更多相关文章

  1. 面localStorage用作数据缓存的简易封装

    面localStorage用作数据缓存的简易封装 最近做了一些前端控件的封装,需要用到数据本地存储,开始采用cookie,发现很容易就超过了cookie的容量限制,于是改用localStorage,但 ...

  2. AVAudioPlayer简易封装

    AVAudioPlayer简易封装 [说明] AVAudioPlayer简易封装,仅仅支持播放,暂停,停止,暂停时候带有渐隐效果,自己用,没有参考价值. [源码] https://github.com ...

  3. 简易封装一个带有占位文字的TextView

    在实际iOS应用开发中我们经常会用到类似于下图所示的界面,即带有占位文字的文本框:

  4. 对xlslib库与libxls库的简易封装

    一.简介 xlslib库是用来创建excel文件.libxls是用来读取excel文件的,在使用C++或者QT语言来设计对excel文件的读取.都需要事先下载这两个库编译成功后再进行程序设计的.之所以 ...

  5. node.js + mssql 简易封装操作

    时间吧,总是这么凑巧,在我学习[node.js]还没几天,我的 Microsoft SQL Server Management Studio 18 就歇菜了,至于怎么歇菜的吧....它可能的意思就是想 ...

  6. 基于Vue简易封装的快速构建Echarts组件 -- fx67llQuickEcharts

    fx67llQuickEcharts A tool to help you use Echarts quickly! npm 组件说明 这本来是一个测试如何发布Vue组件至npm库的测试项目 做完之后 ...

  7. jQuery版AJAX简易封装

    开发过程中,AJAX的应用应该说非常频繁,当然,jQuery的AJAX函数已经非常好用,但是小编还是稍微整理下,方便不同需求下,可以简化输入参数,下面是实例代码: $(function(){ /** ...

  8. 页面localStorage用作数据缓存的简易封装

    最近做了一些前端控件的封装,需要用到数据本地存储,开始采用cookie,发现很容易就超过了cookie的容量限制,于是改用localStorage,但localStorage过于简单,没有任何管理和限 ...

  9. Nhibernate基础使用教程以及简易封装

    1.Nhibernate简介 NHibernate是一个面向.NET环境的对象/关系数据库映射工具.对象/关系数据库映射(object/relational mapping,ORM)这个术语表示一种技 ...

随机推荐

  1. java 把被检查的异常转换为不检查的异常

    一.当我们不知道该怎么处理这个异常,但是也不想把它"吞"了,或者打印一些无用的信息,可以使用异常链的思路解决.可以直接报"被检查的异常"包装进RuntimeEx ...

  2. DDD领域模型企业级系统Unity(五)

    添加程序集: 写一个接口: public interface IPlayer { void Play(); } 两个实现类: public class NewPlay : IPlayer { publ ...

  3. linux内核内存分配(三、虚拟内存管理)

    在分析虚拟内存管理前要先看下linux内核内存的具体分配我開始就是困在这个地方.对内核内存的分类不是非常清晰.我摘录当中的一段: 内核内存地址 ============================ ...

  4. ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)

    XAMPP目录结构 重要的文件和目录 文件/目录                              用途 /opt/lampp/bin/ XAMPP 命令库.例如 /opt/lampp/bin ...

  5. NET MVC Bundling and RequireJS

    NET MVC Bundling and RequireJS   高手速来围观帮忙解惑~关于ASP.NET MVC Bundling and RequireJS的取舍问题,最近比较困惑,我希望有一种方 ...

  6. 005.LVM删除

    一 删除LVM步骤 卸载挂载点 移除LV 移除VG 移除PV 删除配置文件 二 卸载挂载点 [root@kauai ~]# umount /dev/vg01/lv01 #先卸载挂载点 三 移除LV [ ...

  7. Qt代码创建分隔条

    我们在使用Qt时,会发现在使用UI界面时可以添加窗口分隔条,但我们使用纯代码时却没有分隔条的类函数.这时候我们就需要手动添加,添加方式和Qt自定义标签按钮一样,下面我直接写代码.如果不知道怎么添加的, ...

  8. HttpServlet Service方法

    service() 方法是执行实际任务的主要方法.Servlet 容器(即 Web 服务器)调用 service() 方法来处理来自客户端(浏览器)的请求,并把格式化的响应写回给客户端. 每次服务器接 ...

  9. kotlin 安装 使用

    插件下载 下载 kotlin 扩展 . 可以 简写 findviewbyid 这些. 比如 id 是 textview 直接 这样赋值 textview.setText("测试文字" ...

  10. [USACO08OCT]Watering Hole

    [USACO08OCT]Watering Hole 题目大意: Farmer John 有\(n(n\le300)\)个牧场,他希望灌溉他的所有牧场.牧场编号为\(1\sim n\),要灌溉一个牧场有 ...