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. jenkins 2:用ssh agent插件在pipeline里实现scp和远程执行命令

    昨晚测试成功了. 现在ssh agent的认证,已不支持明文用户密码,而只能用加密方式实现. 所以我先在jenknis和nginx服务器之后,实现ssh免密码rsa证书登陆. 私钥放jenkins,公 ...

  2. Java第三阶段学习(十、XML学习)

    一.XML学习 1.模拟Servlet执行 在学习完前端及java与数据库后,将进行WEB编程阶段的学习.在WEB编程中,可以通过浏览器访问WEB服务器上的数据.这时WEB服务器就相当于另一台计算机. ...

  3. kaldi 三个脚本cmd.sh path.sh run.sh

    参考   kaldi 的全部资料_v0.4 cmd.sh 脚本为: 可以很清楚的看到有 3 个分类分别对应 a,b,c.a 和 b 都是集群上去运行这个样子, c 就是我们需要的.我们在虚拟机上运行的 ...

  4. 【BZOJ】2289: 【POJ Challenge】圆,圆,圆

    题解 二分一个横坐标,过这个横坐标做一条和y轴平行的直线,相当于在这条直线上做区间覆盖,如果区间有交的话,那么答案是True 否则的话取两个不相交的区间,如果这两个圆相离或相切则不合法 否则看看相交的 ...

  5. Codeforces 336D Dima and Trap Graph 并查集

    Dima and Trap Graph 枚举区间的左端点, 然后那些左端点比枚举的左端点小的都按右端点排序然后并查集去check #include<bits/stdc++.h> #defi ...

  6. 【转】java取整和java四舍五入方法

    java取整和java四舍五入方法 import java.math.BigDecimal; import java.text.DecimalFormat; public class TestGetI ...

  7. [ 转载 ] Android JNI(一)——NDK与JNI基础

    Android JNI(一)——NDK与JNI基础 隔壁老李头 关注  4.4 2018.05.09 17:15* 字数 5481 阅读 11468评论 8喜欢 140 本系列文章如下: Androi ...

  8. Django Model._meta API

    Model._meta API是Django ORM的核心,它使得lookups.queries.forms.admin这些模块通过每个model类的_meta的属性可以了解每个model的情况. 1 ...

  9. [BZOJ3757]苹果树(树上莫队)

    树上莫队共有三种写法: 1.按DFS序列分块,和普通莫队类似.常数大,不会被卡. 2.按块状树的方式分块.常数小,会被菊花图卡到O(n). 3.按[BZOJ1086]王室联邦的方式分块.常数小,不会被 ...

  10. hdu 3289 最大独立集

    题意:一个动物园里有N只猫和K只狗,一些小朋友来参观,他们如果喜欢狗就不喜欢猫,喜欢猫就不喜欢狗,园长想要移走一些动物,如果,移走的是某个小朋友不喜欢的,而喜欢的没被移走,该小朋友就会高兴,求移动的数 ...