功能列表
1.无输入限制;
2.只能输入数字和字母;
3.只能输入字母;
4.只能输入数字;
5.输入的字母自动转换为大写(解决插入文本后光标跑到最后的问题);
6.过滤黏贴数据(解决iOS粘贴自动添加空格的问题)
实现原理
1.头文件
//
// CPCheckTextField.h
// CPTools
//
// Created by wangzhangchuan on 2018/4/12.
// Copyright ? 2018年 Captain. All rights reserved.
//
#import "CPView.h"
typedef NS_ENUM(NSInteger, CPTextFiledDataType) {
CPTextFiledDataTypeAll, // 无限制
CPTextFiledDataTypeAlphabet, // 字母
CPTextFiledDataTypeNum, // 数字
CPTextFiledDataTypeNumDecimal, // 浮点数
CPTextFiledDataTypeNumAlphabet, // 数字字母
CPTextFiledDataTypeOther // 预留
};
@protocol CPCheckTextFieldDelegate<NSObject>
@optional
- (void)cp_textFieldDidBeginEditing:(UITextField *)textField;
- (void)cp_textFieldDidEndEditing:(UITextField *)textField;
@end
@interface CPCheckTextField : CPView<UITextFieldDelegate>
@property (nonatomic, weak) id <CPCheckTextFieldDelegate> delegate;
// 文本最大长度
@property (nonatomic, assign) NSInteger maxContentLength;
//键盘类型
@property (nonatomic, assign) UIKeyboardType keyboardType;
//是否密码输入模式
@property (nonatomic, assign) BOOL secureTextEntry;
//错误信息提示内容
@property (nonatomic, copy) NSString *errorMsg;
//自动大写模式
@property (nonatomic, assign) UITextAutocapitalizationType autocapitalizationType;
//数据类型
@property (nonatomic, assign) CPTextFiledDataType dataType;
//是否需要大写
@property (nonatomic, assign) BOOL isUpercaseString;
@end
2.实现
#pragma mark - textfield delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([string isEqualToString:@""]) {
return YES;
}
// 总的字符串不能超过 maxContentLength
if (textField.text.length + string.length > self.maxContentLength) {
return NO;
}
NSMutableCharacterSet *cs = [NSMutableCharacterSet characterSetWithCharactersInString:@""];
switch (self.dataType) {
case CPTextFiledDataTypeAlphabet:
{
[cs addCharactersInString:ALPHABET];
}
break;
case CPTextFiledDataTypeNum:
{
[cs formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
}
break;
case CPTextFiledDataTypeNumDecimal:
{
[cs formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
if (![textField.text containsString:@"."]) {
[cs addCharactersInString:@"."];
}
}
break;
case CPTextFiledDataTypeNumAlphabet:
{
[cs addCharactersInString:ALPHABET];
[cs formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
}
break;
default:
break;
}
NSString *filtered = [[string componentsSeparatedByCharactersInSet:[cs invertedSet]] componentsJoinedByString:@""];
if ([filtered isEqualToString:string] == NO) {
// 处理粘贴自动添加的前/后空格
NSMutableString *tempStr = [[NSMutableString alloc] initWithString:string];
if ([tempStr hasPrefix:@" "]) {
[tempStr deleteCharactersInRange:NSMakeRange(0, 1)];
}
if ([tempStr hasSuffix:@" "]) {
[tempStr deleteCharactersInRange:NSMakeRange(tempStr.length - 1, 1)];
}
BOOL valied = [filtered isEqualToString:tempStr];
if (valied == YES) {
// 自动转为大写
if (self.isUpercaseString) {
return [self cp_upercaseSting:range replacementString:tempStr];
}
} else {
return NO;
}
} else {
// 自动转为大写
if (self.isUpercaseString) {
return [self cp_upercaseSting:range replacementString:string];
}
return YES;
}
return YES;
}
/**
将字母转换为大写
@param range 插入位置
@param string 要处理的字符串
@return YES:使用原字符 NO:使用处理过的字符
*/
- (BOOL)cp_upercaseSting:(NSRange )range replacementString:(NSString *)string {
NSMutableString *tempStr = textField.text.mutableCopy;
[tempStr insertString:string atIndex:range.location];
textField.text = [tempStr uppercaseString];
// 光标指定到指定位置
UITextPosition *starP = [textField positionFromPosition:textField.beginningOfDocument offset:range.location + 1];
UITextPosition *endP = [textField positionFromPosition:textField.beginningOfDocument offset:range.location + 1];
UITextRange *p = [textField textRangeFromPosition:starP toPosition:endP];
[textField setSelectedTextRange:p];
return NO;
}
代码 Git地址